DEV Community

Onaolapo-11
Onaolapo-11

Posted on

Plotting a Sine function in Python using Matplotlib and NumPy, and adding Grid lines.

I completed the reading on Day 60.

Day 59 [October 5, 2025]

I need to buckle down, as I’m still lagging on day 2’s remaining goals, day 3 & 4 goals, “Day 3–4: Control structures (if-else, loops)”, as well as day 5 (and 6) goals, “Day 5–6: Functions and modules”, and Day 7 target (exercises) (Meta AI, personal communication, August 8, 2025). If I haven’t covered this, I can’t make progress on day 8–58 goals.

Goals:

As extracted from the ‘Python for Software Development’ textbook by Halvorsen (n.d.):

  • The New Age of Programming ✅

  • What is Python? ✅

  • Introduction to Python ✅

  • Interpreted vs. Compiled ✅

  • Python Packages ✅

  • Python Packages for Science and Numerical Computations ✅

  • Python Editors ✅

  • Python IDLE ✅

  • Visual Studio Code ✅

  • Variables ✅

  • Numbers ✅

  • Strings ✅

  • String Input✅

  • Built-in Functions✅

  • Python Standard Library✅

  • Using Python Libraries, Packages and Modules✅

  • Python Packages✅

  • Plotting in Python

  • Subplots

  • Exercises

  • If … Else

  • Arrays

  • For Loops

  • Nested

  • For Loops

  • While Loops

  • Exercises

  • Creating Functions in Python — Introduction

  • Functions with multiple return values

  • Exercises

  • Creating Classes in Python

  • The init () Function

  • Exercises

  • Creating Python Modules

  • Exercises

Notes:

‘Python for Software Development’ textbook by Halvorsen (n.d.):

To plot a Sine function in Python using matplotlib and numpy (primary code source is Halvorsen, n.d.),

import matplotlib.pyplot as plt

import numpy as np

Create your variables (independent, x and dependent, y)

Essentially plot x against y.

xstart = 0

xstop = 2*np.pi

increment = 0.1

x = np.arange(xstart, xstop, increment) #np.arange fiinds use when an array of #values which are spaced uniformly needs to be generated

y = np.sin(x)

plt.plot(x,y)

plt.xlabel(‘x’)

plt.ylabel(‘y’)

plt.grid() #To add grid lines (w3schools, n.d.)

plt.show()

The grid line to show can be specific, e.g. to show only x-axis grid lines, etc. (w3schools, n.d.). To do this the axis parameter is used, as shown (w3schools, n.d.):

plt.grid(axis = ‘x’)

Line properties such as color, style and width can also be configured for grids, as shown below (w3schools, n.d.):

plt.grid(linestyle = ‘ — -’, color = ‘red’, linewidth = 2)

Inputting the above codes together:

import matplotlib.pyplot as plt

import numpy as np

xstart = 0

xstop = 2*np.pi

increment = 0.1

x = np.arange(xstart, xstop, increment)

y = np.sin(x)

plt.plot(x,y)

plt.xlabel(‘x’)

plt.ylabel(‘y’)

plt.grid(axis = ‘x’, linestyle = ‘dashdot’, color = ‘red’, linewidth = 2)

plt.show()

Output is shown in the image (Figure 1):

Press enter or click to view image in full size

Figure 1. Plotting a Sine function in Python using Matplotlib and NumPy, and adding Grid lines

Although numpy.arange() function and Python’s built-in range() function have similarities, they differ in the sense that, unlike the range() function which returns a list, numpy.arange() function returns a NumPy array (geeksforgeeks, 2025).

What’s a NumPy array?

Summary:

Plotting a Sine function in Python using Matplotlib and NumPy, and adding Grid lines.

*References:
*

  1. geeksforgeeks. (2025, January 24). numpy.arange() in Python. https://www.geeksforgeeks.org/python/numpy-arrange-in-python/

  2. Halvorsen, H. (n.d.). Python. https://halvorsen.blog/documents/programming/python/python.php#python4

  3. w3schools. (n.d.). Matplotlib adding grid lines. https://www.w3schools.com/python/matplotlib_grid.asp

Top comments (0)