How to plot simple parabola using matplotlib in Python

In this tutorial, we are going to learn how to plot a parabola in Python. To show the plotting of the graph on digital systems(computers) we need some sort of functions and libraries. so, this is where we make use of the matplotlib module.

  • First, we need to understand what exactly is the matplotlib.
  • matplotlib is a Python library for data visualization.
  • It creates 2d graphs and plots by using Python scripts.
  • It is simple and basic that is, it will have the data and we compute the date into the computer memory. Now once the computer has drawn the data we can show it.

Plot a simple parabola using matplotlib in Python

To plot the graphs in Python we use the popular library called matplotlib.

It will have the same kind of classes and objects when we try to refer to the matplotlib module. That’s why we import matplotlib.pyplot where matplotlib is basic class and pyplot is a function of it.

we can integrate this by using numpy or pandas.

The below code will draw the simple parabola y=x**2(X square).

from matplotlib.pyplot import *
from numpy import *
x=linspace(-1,1,5000)
y=x**2
plot(x,y)
xlabel("x axis")
ylabel("y axis")
print(x)
show()

First, we import the matplotlib library and also we can import numpy for linspace and other functions.

The function we used is linspace which has 3 parameters where first is initial value next final value and 3rd parameter is the total number of intervals.

Next, we use the plot function to plot the x, y coordinates. x label and y label are used to show the x-axis and y-axis.

Finally, we use the show() function to view the graph.

Output:

plot simple parabola using matplotlib in Python

We can observe the intervals from -1 to 1 from the output using the print() function in the source code.

So, this how we got the simple parabola.

We can also plot another parabola equation. Let us take y2(y square)=x

Source code:

from matplotlib.pyplot import *
from numpy import *
y=linspace(-1,1,5000)
x=y**2
plot(x,y)
show()

Output:

plot simple parabola using matplotlib in Python

Also read:

Leave a Reply

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