Plotting Equations with Python in Matplotlib

In this article, we are going to cover the plotting of some basic equations in Python using matplotlib module. The goal is to plot a function in x i.e y  = f(x). Example y = x2, y = x2+2x+3 , y = 2sin(x)+4 etc.

Before jumping directly on how to do this, let us learn the overview of how to plot these equations. Consider these points as a skeleton to the solution to this problem.

  • Learn how to create a vector array using the NumPy library.
  • Change and manipulate this vector to match the required equation.
  • Plot this vector using the matplotlib library.

Now let’s start with the plotting of the simplest equation i.e y = x2.

Plotting y = x2

As y is a function of x where each y (y1, y2, y3 ……….yn)  is equal to the square of its x counterpart. So first we need to define an array x  ranging from some initial point to a final point, for example  [-10,10]. We will then calculate y array by squaring each element of the array x. This can be easily done with the help of NumPy. NumPy allows arithmetic operations on the NumPy arrays directly with ease. Look at the code below to plot the above function and try to understand the code by reading the comments.

# Importing the libraries
import matplotlib.pyplot as plt
import numpy as np

# Creating the vector array x ranging from [-10,10]
x = np.linspace(-10,10,100)

# Manipulating the vector x to produce y=x^2
y = x**2

# Setting the figure size
fig=plt.figure(figsize=(8,6))

#Plotting the matplotlib plot with x and y as parameters
plt.plot(x,y,label='y = x^2',color='green')

# Adding a grid
plt.grid(alpha=.5,linestyle='-')

# Naming the x and the y axis
plt.xlabel('x-axis')
plt.ylabel('y-axis')

# Giving a title to the plot
plt.title('Plotting a equation')

# Adding the legend 
plt.legend()

plt.show()

Plotting Equations 1

As you can see we follow the steps that I mentioned at the starting of the article. Let plot some more plots with the same method with slight variations.

Plotting y = Sin(x) + x

We can plot this graph using the same steps. See the code below which plot y = sin(x) + x in the range [-2*pi , 2*pi].

# Importing the libraries
import matplotlib.pyplot as plt
import numpy as np

# Creating the vector array x ranging from [-2*pi,2*pi]
x = np.linspace(-2*np.pi,2*np.pi,100)

# Manipulating the vector x to produce y= sin(x)+x
y = np.sin(x)+x

# Setting the figure size
fig=plt.figure(figsize=(8,6))

#Plotting the matplotlib plot with x and y as parameters
plt.plot(x,y,label='y = sin(x)+x',color='red')

# Adding a grid
plt.grid(alpha=.5,linestyle='-')

# Naming the x and the y axis
plt.xlabel('x-axis')
plt.ylabel('y-axis')

# Giving a title to the plot
plt.title('Plotting a equation')

# Adding the legend 
plt.legend()

plt.show()

Plotting Equations 2

Some cool graphs

Now let’s try some cool graphs with interesting equations. See the code below and read the comments to understand the equation and type of graph made.

# Importing the libraries
import matplotlib.pyplot as plt
import numpy as np

# Creating the vector array x1 ranging from [-2*pi,2*pi]
x1 = np.linspace(-2*np.pi,2*np.pi,100)

# Manipulating the vector x1 to produce y1= sin(954x)-2cos(x)
y1 = np.sin(954*x1)-2*np.cos(x1)

# Creating the vector array x2 ranging from [0,3]
x2 = np.linspace(0,3,100)

# Manipulating the vector x2 to produce y2 = |Sin(x^x)/2^((x^x-pi/2)/pi)|
y2 = abs((np.sin(x**x))/(2**((x**x-(np.pi/2))/np.pi)))

# Setting the figure size
fig=plt.figure(figsize=(12,12))

# Subplot 1 for x1 and y1
plt.subplot(2, 1, 1)
plt.plot(x1, y1,label='y = sin(954x)-2cos(x)',color='red')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.grid(alpha=.5,linestyle='-')
plt.title('Plotting a equation')
plt.legend()

# Subplot 2 for x2 and y2
plt.subplot(2, 1, 2)
plt.plot(x2, y2,label='y = |Sin(x^x)/2^((x^x-pi/2)/pi)|',color='green')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.grid(alpha=.5,linestyle='-')
plt.title('Plotting a equation')
plt.legend()

plt.show()

Plotting Equations 3

I hope you liked the article. Comment if you have any doubts or suggestions regarding this article.

You can also read other articles related to this. Click the links given below.

 

Leave a Reply

Your email address will not be published. Required fields are marked *