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