8.1.2. Pylab background

We are learning how to use Python and numpy and matplotlib. All are excellent computational tools for all kinds of scientific, mathematical, and computing purposes

  1. IPython notebook demoing pylab/matplotlib: Collection of various Pylab/Matplotlib demo scripts
  2. Basic intro inspiring some of the examples discussed here: Scipy matplotlib plotting tutorial.
  3. Shorter prelude tutorial: Nicolas Rougier’s 2012 Euroscipy tutorial.
  4. For a really nice video intro to numpy and matplotlib, see Eric Jones’s great tutorial
  5. For the visually impaired who (like me) want to work through a well-structured written introduction with downloadable examples, see this Scipy lecture by Nicolas Rougier, Mike Müller, and Gaël Varoquaux.
  6. For more details on how to use pyplot (the matplotlib component you use here), go here, but be patient. The documentation and examples are not well-organized and it helps to approach them with some prior knowledge.
  7. For a very focused and related set of recipes that should be read in sequence, helpful once you’ve got the basics, go to the Matplotlib recipes page.
  8. For a set of unrelated matplotlib recipes, go to the matplotlib recipes pages
  9. For a great website providing MAcOS framework binaries for a host of image/GIS libraries (including the open source Geometry engine GEOS ), look in the Kyng Chaos Wiki
  10. For very hasty introduction for those just trying to get the assignment done, see below.

8.1.2.1. Basic matplotlib concepts

Note

The concepts discussed here are demoed in more detail in a notebook .ipynb

In matplotlib parlance, the Python object corresponding to the entire canvas on which the graph is plotted is called a figure. A figure may have one or more subplots (it is often convenienet to arrange plots side by by side or in groups). The subplots are called axes.

To start plotting, decide on the number of subplots and their arrangement (2 by 3, 4 by 1, etcetera) and call subplots (note the ‘s’):

>>> fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)

This gives you figure with 4 plots arranged in a 2x2 square.

8.1.2.2. Simple pylab demo

Here is a quick demo of how to use pylab and save the results:

from pylab import *

# make a square figure and axes
figure(1, figsize=(6,6))

plot([1,2,3,4,5,6,7,8,9,10], [1,4,9,16,25,36,49,64,81,100])
title('Quadratic', bbox={'facecolor':'0.8', 'pad':10})

show()

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

../_images/sample_plots.png

Use show() if you want to see the figure. if you dont want to show the figure, you just save it as a file with this command, which must be included before the show command.:

savefig('quadratic.png')

The show command brings up the same kind of graph window you saw above with NLTK frequency distributions. You can save your graph the same way you did above.

To use loglog axes replace the plot command with the loglog command:

from pylab import *

# make a square figure and axes
figure(1, figsize=(6,6))

loglog([1,2,3,4,5,6,7,8,9,10], [1,4,9,16,25,36,49,64,81,100],basex=10,basey=10)
title('LogQuadratic', bbox={'facecolor':'0.8', 'pad':10})
show()

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

../_images/sample_plots_2.png

Having nothing to do with this exercise, here’s a more complex example with a pie chart:

from pylab import *

# make a square figure and axes
figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]

##  The highlights the hogs wedge, pulling it out slightly from the pie.
explode=(0, 0.05, 0, 0)
pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})

show()

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

../_images/sample_plots_3.png