Matplotlib

Matplotlib is the de facto 2D plotting interface for python, thanks to its integration with ipython. It produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms (Tk, GTK, Qt, ...).

Matplotlib comes with two programming interfaces:

  • The pyplot programatic interface which is an object oriented interface with figure/axes/plots.
  • The pylab interface which provides a matlab like interface in ipython. Have you noticed the %pylab magic command ?

In [1]:
from scipy.misc import lena
img = lena()

This is just a way to produce an image ... here the well-known picture from Lena

Programatic interface

The pyplot interface is the most powerful, feature rich interface to use matplotlib. It provides an object oriented interface where every object is under control (nothing is hidden)


In [2]:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.imshow(img, cmap="gray")
ax2.hist(img.flat, 256)
fig.show()

There are plenty of plots available among them:

  • Line plots: plot, semilogx, semilogy, loglog
  • Scatter plots: scatter
  • Histograms: hist
  • Bar charts: bar
  • Pie charts: pie
  • Images: imshow
  • Contour plots: contour

Of course many other types of plots are possible, the easiest is probably to look at the gallery

Some other interesing commands are:

  • figure.clf(): clear figure
  • axe.cla(): clear axis
  • figure.show(): display the figure
  • figure.canvas.draw(): enforce the update of the image drawn
  • figure.savefig(fname): save the image (as PNG by default)

Overall, this lacks interactivity !

Matlab-like interface: pylab

The pylab interface offers a scripting user interface close to the one of Matlab, especially when integrated into ipython.

One can select the backend at runtime like %pylab inline or %pylab qt4

The pylab interface makes common matplotlib interface for the former action as simple as:

  • clf(): Clear the current figure
  • cla(): Clear the current plot (“clear axes”)
  • subplot(rci) or subplot(r,c,i): Divide plot area in r x c plots and select I as current
  • show(): Update the plot
  • savefig(fname): - Save the plot to a file
  • ginput(n): Recover (x,y) coordinates of n user clicks

This is useful for prototyping and create small scripts. I recommand you to use the %pylab in ipython


In [3]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [4]:
x = numpy.linspace(-5,5,500)
y = numpy.exp(-x*x)
plot(x,y,label="gaussian")
legend()


Out[4]:
<matplotlib.legend.Legend at 0x7fe6ab8180d0>

Warning, this "ease of use" comes a price:

  • Many objects are hidden and get never freed
  • Showing arrays as images usually creates 3 extra copies:
    • a copy to prevent modification
    • the same represented as float between 0 and 1
    • the same in 3 x uint8 after applying the colormap
  • Huge memory consumption, which ends in memory-leaks

The authors are aware of this "weakness" ... but this is the price to mimic matlab. Anyway, by using matlab you would have to by the same amount of memory for your computer in addition to the license :þ

If you run ipython --pylab what happends ?

Python performs a *from pylab import **


In [5]:
matplotlib.pylab??

Scarry, no ?

Conclusion

Use the pylab interface for prototyping interactively and convert it to the pyplot interface when writing the application.

Visualization may be OK, but Matlab provides me with all libraries I have dreamt of. Aren't they available from Python ? Yes they are available within SciPy


In [ ]: