Pylab background

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. For a really nice video intro to numpy and matplotlib, see Eric Jones's great tutorial here.
  2. 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.
  3. 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.
  4. For a set of matplotlib recipes, go here
  5. For a great website providing MAcOS framework binaries for a host of image/GIS libraries (including the open source Geometry engine GEOS) look here.
  6. For very hasty introduction for those just trying to get the assignment done, see below.

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()
# 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.
#  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()


from pylab import *

# make a square figure and axes
figure(1, figsize=(6,6))
x = range(1,11)
y = [100./n for n in x]
loglog(x,y,basex=10,basey=10)
show()

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()