9.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.

9.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.

9.1.2.2. Simple pylab demo

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

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:

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