Matplotlib is an object-oriented plotting library for python. It is a MATLAB/Scilab-like application programming interface (API) and provides accurate high-quality figures, which can be used for publication purposes.
matplotlib contains pylab interface, which is the set of functions provided by matplotlib.pylab to plot graph. matplotlib. pyplot is a collection of command-style functions that helps matplotlib to work like MATLAB.
Let us install Matplotlib
To start a plotting experiment, first we need to import matplotlib.pylab
>>>import matplotlib.pyplot as plt
Here library - matplotlib.pyplot - is imported and labeled as plt for easy future reference of the module.
>>>import matplotlib.pyplot as plt >>>plt.plot([ 1 , 2, 3 ,4 ], [ 4 ,3 , 2, 1 ]) >>>plt.axis([ 0 , 5 , 0 , 5]) >>>plt.show()
The plot function accepts the plotting points as two arrays with x, y coordinate respectively. Pyplot fits a straight line to the points. If you need only a scatter diagram of the points try the following code.
>>>plt.plot([ 1 , 2, 3 ,4 ], [ 4 ,3 , 2, 1 ], 'ro')
You can plot the graph using different colors and styles by putting an argument after the plot function.
import matplotlib.pyplot x=arange(1.,10.,0.1) y=x*x plot(x,y,'g--') show()
After plotting the graph, to view it, you need to type show()command.
Here you will get a green line graph; try with r for red, y for yellow etc. We can specify shapes with cryptic reference such as S for square, ^ for triangle etc.
>>plot(x,y,'rs') # Red square >>plot(x,y,'g^') # Green triangle
Standard mathematical function can also be plotted. Let us plot sine curve.
from pylab import * x = arange(0.,10.,0.1) # to define x values y = sin(x) # function definition plot(x,y) # to plot grid(True) # to show graph in grid show() # to show the plot
To Save plot use code
plt.savefig('sine_curve.png')
# Draw two sets of points plt.plot([1,2,3,4,5], [1,2,3,4,10], 'go') # green dots plt.plot([1,2,3,4,5], [2,3,4,5,11], 'b*') # blue stars plt.show()
Lets add some more details
plt.plot([1,2,3,4,5], [1,2,3,4,10], 'go', label='GreenDots') plt.plot([1,2,3,4,5], [2,3,4,5,11], 'b*', label='Bluestars') plt.title('A Simple Scatterplot') plt.xlabel('X') plt.ylabel('Y') plt.legend(loc='best') # legend text comes from the plot's label parameter. plt.show()
Now Lets plot this in two different subplots
plt.figure(figsize=(10,4), dpi=120) # 10 is width, 4 is height# Left hand side plot plt.subplot(1,2,1) # (nRows, nColumns, axes number to plot) plt.plot([1,2,3,4,5], [1,2,3,4,10], 'go') # green dots plt.title('Scatterplot Greendots') plt.xlabel('X')
plt.ylabel('Y') plt.xlim(0, 6)
plt.ylim(0, 12)# Right hand side plot plt.subplot(1,2,2) plt.plot([1,2,3,4,5], [2,3,4,5,11], 'b*') # blue stars plt.title('Scatterplot Bluestars') plt.xlabel('X')
plt.ylabel('Y') plt.xlim(0, 6)
plt.ylim(0, 12) plt.show()
Different Styles in Matplotlib
>>> plt.style.available
Custumizing legents
plt.style.use('ggplot')
plt.figure(figsize=(10,7), dpi=80)
X = np.linspace(0, 2*np.pi, 1000)
sine = plt.plot(X,np.sin(X))
cosine = plt.plot(X,np.cos(X))
sine_2 = plt.plot(X,np.sin(X+.5))
cosine_2 = plt.plot(X,np.cos(X+.5))
plt.gca().set(ylim=(-1.25, 1.5), xlim=(-.5, 7))
plt.title('Custom Legend Example', fontsize=18)
# Modify legend
plt.legend([sine[0], cosine[0], sine_2[0], cosine_2[0]], # plot items
['sine curve', 'cosine curve', 'sine curve 2', 'cosine curve 2'],
frameon=True, # legend border
framealpha=1, # transparency of border
ncol=2, # num columns
shadow=True, # shadow on
borderpad=1, # thickness of border
title='Sines and Cosines') # title
plt.show()