For plotting equations we will use two modules Matplotlib.pyplot and Numpy. This module helps you to organize your Python code logically.
Numpy is a core library used in Python for scientific computing. This Python library supports you for a large, multidimensional array object, various derived objects like matrices and masked arrays, and assortment routines that makes array operations faster, which includes mathematical, logical, basic linear algebra, basic statistical operations, shape manipulation, input/output, sorting, selecting, discrete Fourier transforms, random simulation and many more operations.
Note: For more information, refer to NumPy in Python
Matplotlib is a plotting library of Python which is a collection of command style functions that makes it work like MATLAB. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits. Each #pyplot# function creates some changes to the figures i.e. creates a figure, creating a plot area in the figure, plotting some lines in the plot area, decoration of the plot with some labels, etc.
Note: For more information, refer to Pyplot in Matplotlib
Plotting the equation
Lets start our work with one of the most simplest and common equation Y = X². We want to plot 100 points on X-axis. In this case, the each and every value of Y is square of X value of the same index.
Here note that the number of points we are using in line plot(100 in this case) is totally arbitrary but the goal here is to show a smooth graph for a smooth curve and that’s why we have to pick enough numbers depending on on the function. But be careful that do not generate too many points as a large number of points will require a long time for plotting.
Customization of plots
There are many pyplot functions are available for the customization of the plots, and may line styles and marker styles for the beautification of the plot.Following are some of them:
|plt.xlim()||sets the limits for X-axis|
|plt.ylim()||sets the limits for Y-axis|
|plt.grid()||adds grid lines in the plot|
|plt.title()||adds a title|
|plt.xlabel()||adds label to the horizontal axis|
|plt.ylabel()||adds label to the vertical axis|
|plt.axis()||sets axis properties (equal, off, scaled, etc.)|
|plt.xticks()||sets tick locations on the horizontal axis|
|plt.yticks()||sets tick locations on the vertical axis|
|plt.legend()||used to display legends for several lines in the same figure|
|plt.savefig()||saves figure (as .png, .pdf, etc.) to working directory|
|plt.figure()||used to set new figure properties|
|–||solid line style|
|—||dashed line style|
|-.||dash-dot line style|
|:||dotted line style|
Below is a plot created using some of this modifications:
Plotting a graph of the function y = Cos(x) with its Taylor polynomials of degree 2 and 4.
Generating an array of 10000 random entries sampled from normal distribution and creating a histogram along with a normal distribution of the equation:
- Evaluate the Mathematical Expressions using Tkinter in Python
- Box Plot in Python using Matplotlib
- Plot 2-D Histogram in Python using Matplotlib
- Simple Plot in Python using Matplotlib
- Plot a pie chart in Python using Matplotlib
- Tri-Surface Plot in Python using Matplotlib
- Contour Plot using Matplotlib - Python
- Plot a quadrilateral mesh in Python using Matplotlib
- Make a violin plot in Python using Matplotlib
- Plot the magnitude spectrum in Python using Matplotlib
- Matplotlib.pyplot.plot() function in Python
- Plot the phase spectrum in Python using Matplotlib
- Plot the power spectral density using Matplotlib - Python
- Create a pseudocolor plot of an unstructured triangular grid in Python using Matplotlib
- Matplotlib.axes.Axes.plot() in Python
- Bar Plot in Matplotlib
- Quiver Plot in Matplotlib
- Autocorrelation plot using Matplotlib
- Python | Mathematical Median of Cumulative Records
- Pie plot using Plotly in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.