Matplotlib - basics

Introduction

The most frequently used plotting package in Python, matplotlib, is written in pure Python and is heavily dependent on NumPy. The main webpage introduction itemizes what John Hunter (mpl creator) was looking for in a plotting toolkit.

  • Plots should look great - publication quality. One important requirement for me is that the text looks good (antialiased, etc.)
  • Postscript output for inclusion with TeX documents
  • Embeddable in a graphical user interface for application development
  • Code should be easy enough that I can understand it and extend it
  • Making plots should be easy

Matplotlib is conceptually divided into three parts:

  1. pylab interface (similar to MATLAB) – pylab tutorial
  2. Matplotlib frontend or API – artist tutorial
  3. backends – drawing devices or renderers

Essentials

The Axes class is the most important class in mpl. The following three lines are used to get an axes class ready for use.

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(2,1,1)

If you need a freehand axis then

>>> fig2 = plt.figure()
>>> ax2 = fig2.add_axes([0.15, 0.1, 0.7, 0.3])   # [left, bottom, width, height]

After a figure is drawn you may save it and or plot it with the following.

>>> fig.saveas('foo.png',dpi=200)
>>> plt.show()

The DPI argument is optional and we can save to a bunch of formats like: JPEG, PNG, TIFF, PDF and EPS.

Here is the example from the artist tutorial.

An example

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
fig.subplots_adjust(top=0.8)
ax1 = fig.add_subplot(211)
ax1.set_ylabel('volts')
ax1.set_title('a sine wave')

t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*np.pi*t)
line, = ax1.plot(t, s, color='blue', lw=2)

ax2 = fig.add_axes([0.15, 0.1, 0.7, 0.3])
n, bins, patches = ax2.hist(np.random.randn(1000), 50,
    facecolor='yellow', edgecolor='yellow')
ax2.set_xlabel('time (s)')

(Source code, png, hires.png, pdf)

../_images/StandardMplExample.png