Matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell, web application servers, and six graphical user interface toolkits.
Matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc, with just a few lines of code. For a sampling, see the screenshots, thumbnail gallery, and examples directory
For simple plotting the pyplot interface provides a MATLAB-like interface, particularly when combined with IPython. For the power user, you have full control of line styles, font properties, axes properties, etc, via an object oriented interface or via a set of functions familiar to MATLAB users.
One of the most appealing advantages of Matplotlib is its versatility. You can use it in a very basic way as well as in a very customizable way, allowing a total control of what we want to sketch. We illustrate here some of the basic functions.
#Importing numpy
import numpy as np
#Ignore me!! (for scripts)
%pylab inline
#Importing matplotlib
import matplotlib.pyplot as plt
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
The line %pylab inline is always necessary when using a IPython notebook. If you are working on the interpreter or with scripts, always use the function plt.show() in order to display the resulting image.
#Ploting a function
X = np.linspace( 0, 2*np.pi, 100 )
Y = np.sin( X )
plt.plot( X, Y )
[<matplotlib.lines.Line2D at 0x30641d0>]
#Scatter of points
X = np.random.random(200)
Y = np.random.random(200)
plt.plot( X, Y, 'o' )
[<matplotlib.lines.Line2D at 0x3c116d0>]
#Multiple plots
#Ploting a function
X = np.linspace( 0, 2*np.pi, 100 )
Y1 = np.sin( X )
Y2 = np.cos( X )
plt.plot( X, Y1 )
plt.plot( X, Y2 )
[<matplotlib.lines.Line2D at 0x3bff410>]
#Logaritmic axis Y
X = np.linspace( 1, 10, 100 )
Y = np.exp( X )
plt.semilogy( X, Y )
[<matplotlib.lines.Line2D at 0x3c25f90>]
#Logaritmic axis X
X = np.linspace( 1, 10, 100 )
Y = np.log( X )
plt.semilogx( X, Y )
[<matplotlib.lines.Line2D at 0x4126ed0>]
#Double Logaritmic axis
X = np.linspace( 0.1, 16.5*np.pi, 1000 )
Y = np.sin( X )**2
plt.loglog( X, Y )
[<matplotlib.lines.Line2D at 0x51d8210>]
#Polar plots
r = np.arange( 0, 3.0, 0.01 )
theta = 2*np.pi*r
plt.subplot(111, polar=True)
plt.plot( theta, r )
[<matplotlib.lines.Line2D at 0x51c0e90>]
#Fill between
X = np.arange( 0, 2, 0.1 )
Y1 = X/2.
Y2 = X**2 + 1
plt.fill_between( X, Y1, Y2 )
<matplotlib.collections.PolyCollection at 0x64b1bd0>
#Multiple figures
X = np.arange( 0, 10, 0.1 )
#First plot
plt.subplot( 1, 2, 1 )
plt.plot( X, X**2+1 )
#Second plot
plt.subplot( 1, 2, 2 )
plt.semilogy( X, np.exp(X-10) )
[<matplotlib.lines.Line2D at 0x7447090>]
#Histograms
X = np.random.random( 500 )
plt.hist( X )
(array([57, 49, 53, 55, 49, 45, 46, 62, 36, 48]), array([ 7.31894984e-04, 1.00384723e-01, 2.00037552e-01, 2.99690380e-01, 3.99343209e-01, 4.98996037e-01, 5.98648866e-01, 6.98301694e-01, 7.97954523e-01, 8.97607351e-01, 9.97260180e-01]), <a list of 10 Patch objects>)
Matplotlib supports complex formatting, even allowing LaTeX expressions. These features makes Matplotlib a very appealing package for making professional figures, for example for a scientific paper. Next it is shown an example (script) where you can see some of the capabilities of Matplotlib
#! /usr/bin/python
#====================================================================
#Importing libraries
#====================================================================
import numpy as np
import matplotlib.pyplot as plt
#Arrays
X = np.linspace( 0, 10, 100 )
Y = X**0.5
Y1 = Y+0.5*(1+np.random.random(100))
Y2 = Y-0.5*(1+np.random.random(100))
#Setting the figure environment
plt.figure( figsize = (16,6) )
#====================================================================
#First plot
#====================================================================
plt.subplot( 1, 2, 1 )
plt.plot( X, Y )
plt.title( "This is an unformatted plot" )
#====================================================================
#Second plot
#====================================================================
plt.subplot( 1, 2, 2 )
#2D Plots
plt.plot( X, Y, linewidth=4, color="green", label="Function" )
plt.plot( X, Y1, linewidth=2, color="green" )
plt.plot( X, Y2, linewidth=2, color="green" )
#Error region
plt.fill_between( X, Y1, Y2, color="green", alpha=0.2 )
#Grid
plt.grid( True )
#X-axis limits
plt.xlim( (0, 10) )
#Y-axis limits
plt.ylim( (0, 5) )
#Label
plt.legend( loc="upper left", fontsize=16 )
#X label
plt.xlabel( "$x$ coordinate", fontsize=20 )
#Y label
plt.ylabel( "$\sqrt{x}$", fontsize=20 )
#Title
plt.title( "This is a formatted plot", fontsize=16 )
#====================================================================
#Showing
#====================================================================
#You can store this figure in any format, even in vectorial formats
#like pdf
plt.savefig( "Figure.pdf" )
#Showing the figure on screen
plt.show()
This gallery comprehends some interesting advanced uses of matplotlib.
from IPython.core.display import Image
Image(filename='./figures/voids.png')
Image(filename='./figures/VPHphase.png')
Image(filename='./figures/simulation.png')
Image(filename='./figures/voiddensity.png')
Image(filename='./figures/halosfraction.png')