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. Show Simple GraphsHere 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. Examplefrom 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() OutputRunning the above code gives us the following result − MultiplotsWe can have two or more plots on a single canvas by creating multiple axes and using them in the program. Exampleimport 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() OutputRunning the above code gives us the following result − Grid of SubplotsWe 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. Exampleimport 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() OutputRunning the above code gives us the following result: Contour PlotContour 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. Exampleimport 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() OutputRunning the above code gives us the following result:
Updated on 17-Oct-2019 13:03:28
Chris Webb Sep 13 5 min read 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). |