How do you plot a math function in python?


Python has the ability to create graphs by using the matplotlib library. It has numerous packages and functions which generate a wide variety of graphs and plots. It is also very simple to use. It along with numpy and other python built-in functions achieves the goal. In this article we will see some of the different kinds of graphs it can generate.

Simple Graphs

Here we take a mathematical function to generate the x and Y coordinates of the graph. Then we use matplotlib to plot the graph for that function. Here we can apply labels and show the title of the graph as shown below. We are plotting the graph for the trigonometric function − tan.

Example

from matplotlib import pyplot as plt
import numpy as np
import math #needed for definition of pi
x = np.arange(0, math.pi*2, 0.05)
y = np.tan(x)
plt.plot(x,y)
plt.xlabel("angle")
plt.ylabel("Tan value")
plt.title('Tan wave')
plt.show()

Output

Running the above code gives us the following result −

How do you plot a math function in python?

Multiplots

We can have two or more plots on a single canvas by creating multiple axes and using them in the program.

Example

import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
fig=plt.figure()
axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # main axes
axes2 = fig.add_axes([0.55, 0.55, 0.3, 0.3]) # inset axes
axes3 = fig.add_axes([0.2, 0.3, 0.2, 0.3]) # inset axes
axes1.plot(x, np.sin(x), 'b')
axes2.plot(x,np.cos(x),'r')
axes3.plot(x,np.tan(x),'g')
axes1.set_title('sine')
axes2.set_title("cosine")
axes3.set_title("tangent")
plt.show()

Output

Running the above code gives us the following result −

How do you plot a math function in python?

Grid of Subplots

We can also create a grid containing different graphs each of which is a subplot. For this we use the function subplot2grid. Here we have to choose the axes carefully so that all the subplots can fit in to the grid. A little hit an dtrail may be needed.

Example

import matplotlib.pyplot as plt
a1 = plt.subplot2grid((3,3),(0,0),colspan = 2)
a2 = plt.subplot2grid((3,3),(0,2), rowspan = 3)
a3 = plt.subplot2grid((3,3),(1,0),rowspan = 2, colspan = 2)
import numpy as np
x = np.arange(1,10)
a2.plot(x, x*x,'r')
a2.set_title('square')
a1.plot(x, np.exp(x),'b')
a1.set_title('exp')
a3.plot(x, np.log(x),'g')
a3.set_title('log')
plt.tight_layout()
plt.show()

Output

Running the above code gives us the following result:

How do you plot a math function in python?

Contour Plot

Contour plots (sometimes called Level Plots) are a way to show a three-dimensional surface on a two-dimensional plane. It graphs two predictor variables X Y on the y-axis and a response variable Z as contours.Matplotlib contains contour() and contourf() functions that draw contour lines and filled contours, respectively.

Example

import numpy as np
import matplotlib.pyplot as plt

xlist = np.linspace(-3.0, 3.0, 100)
ylist = np.linspace(-3.0, 3.0, 100)

X, Y = np.meshgrid(xlist, ylist)
Z = np.sqrt(X**2 + Y**2)

fig,ax=plt.subplots(1,1)
cp = ax.contourf(X, Y, Z)
fig.colorbar(cp) # Add a colorbar to a plot
ax.set_title('Filled Contours Plot')

#ax.set_xlabel('x (cm)')
ax.set_ylabel('y (cm)')
plt.show()

Output

Running the above code gives us the following result:

How do you plot a math function in python?

How do you plot a math function in python?

Updated on 17-Oct-2019 13:03:28

  • Related Questions & Answers
  • Plotting graph using seaborn in python.
  • Plotting a cumulative graph of Python datetimes in Matplotlib
  • Plotting solar image in Python
  • Plotting profile histograms in Python Matplotlib
  • Geographical plotting using Python plotly
  • SunPy Plotting a Solar Image in Python
  • Plotting animated quivers in Python using Matplotlib
  • Plotting Google Map using gmplot package in Python?
  • Plotting grids across the subplots in Python Matplotlib
  • What is the use of type = "h" in base R for plotting a graph?
  • Python - Plotting charts in excel sheet using openpyxl module
  • Interactive plotting with Python Matplotlib via command line
  • Python - Plotting Area charts in excel sheet using XlsxWriter module
  • Python - Plotting bar charts in excel sheet using XlsxWriter module
  • Python - Plotting column charts in excel sheet using XlsxWriter module

How do you plot a math function in python?

Chris Webb

Sep 13

5 min read

How do you plot a math function in python?

There is a wealth of information out there on plotting mathematical functions in Python, typically using NumPy to create a set of y values for a range of x values and plotting them with Matplotlib. In this article I will write a simple but powerful function to abstract away much of the repetitive code necessary to do this. In future articles I will show the code in use for a selection of functions.

How do you plot a function on a graph?

To graph a function, you have to select x-values and plug them into the equation. Once you plug those values into the equation, you will get a y-value. Your x-values and your y-values make up your coordinates for a single point.

Can I plot a graph using Python?

Graphs in Python can be plotted by using the Matplotlib library. Matplotlib library is mainly used for graph plotting. You need to install matplotlib before using it to plot graphs. Matplotlib is used to draw a simple line, bargraphs, histograms and piecharts.

How do you plot a parabola in Python?

How to plot parabola in Python.
Code for python user friendly..
import matplotlib.pyplot as plt. import numpy as np..
x = np.linspace(-50,50,100) y = x**2..
plt.plot(x,y) plt.show().
#plt.axis('equal').
code for matlab user friendly..
from matplotlib.pyplot import *.
from numpy import * x = linspace(-50,50,100).