In this assignment you will learn some the basics of matplotlib
, a
standard plotting library for python.
matplotlib
modulenumpy
commands[30] As a Shakespearean scholar and computer scientist, you want do to
some data analysis of Shakespeare's sonnets. Download shakespeare.txt
here. In lab2a.py
, import the data using
numpy
's loadtxt
command. Use the documentation or the help
command in a
python terminal to find the docstring. (See
this
link.) Then, use the Shakespeare data and matplotlib
to plot the number of
words in a line against the number of characters in a line. You should look at
the documentation for
matplotlib.pyplot,
specifically its plot
function. Label the line. Label the axes. Label the
plot. Include a legend. Place tickmarks for every character. Save the figure as
Shakespeare.png
. You might want to display it to see that it worked as
intended.
[15] Plot two figures, one on top of the other, where the top one shows
f(x) = x^2 - 5
and the other f(x) = x^2 - 7
. Annotate the positive x
intercept. Reset the axes (spines) so that they are in the proper location
relative to the data. Show the subplot tool. Save this plot as Algebra.png
.
Hint: the linspace
command from numpy
will make this much nicer than
generating lists of data points.
[5] Take a look back at part one. Why did you draw a line graph? That was
messy. That wasn't the type of graph you actually wanted. Now that you know more
about graphs, write another copy of the Shakespeare code using a scatter plot.
The documentation will tell you a lot about different types of graphs you could
use. Save the resulting plot as Shakespeare2.png
.
CO2 and other greenhouse gases are bad for the atmosphere. Due to
treaties like Kyoto, many countries have pledged to lower emissions. Much of
this data is easily available from Wikipedia. Data on CO2 emissions by
large emitters has been compiled for you here in the file
co2.txt
.
[15] Import the data using numpy
. Graph the total emissions per
country with a bar graph. Be sure to label pieces of the graph where apropriate.
Save it as Total.png
. If the graph doesn't fit on the screen (there are a lot
of countries) limit yourself to the 5 or 10 largest emitters. Suppose the data
we have is only accurate to within 10 percent. Then add the corresponding error
bars.
[5] The file co2.txt
also has the percent CO2 emissions per
country data. Make a pie graph to represent that data. Save that as
Percent.png
.
Now that we have some data about the largest offenders, let's make something
more creative than a simple plot. It is possible to use subplots to overlay
images in pyplot
. While this could be used to draw, other libraries such as
Tkinter
and pygame
satisfy that niche already. However, there is a
geography module called Basemap
that is built on top of pyplot
. Its
documentation is here.
from mpl_toolkits.basemap import Basemap
fig = plt.figure(figsize=(14,10))
earth = Basemap(projection="mill")
earth.drawcoastlines(color='.3')
earth.fillcontinents(color='.5')
earth.drawcountries(color='.7')
plt.show()
This will create a plot of Earth with the countries and continents drawn in.
We are going to plot the per capita emissions of every country that we have data
for on top of this map. To do so, we will need to create a subplot that takes up
the entirety of the plot and plot
to it after creating Earth.
[30] Plot circles on the countries in question with their radius
proportional to the amount of emissions they produce per capita. You will also
need to know where the countries are. The countries listed in the previous file
have latitude and longitude listed in the file
countries.txt
. Use numpy
again. Remember to
convert latitude and longitude into points that can be used on the figure by
using the Basemap
class.
earth = Basemap(projection="mill")
earth((long, lat))
# Will output a tuple of (x,y) scaling the data for use on this figure.
Choose a reasonable scaling factor for the size of the circles. Save this
figure as Capita.png
.
The lab2a.py
, lab2b.py
, programs and the Shakespeare.png
,
Algebra.png
, Shakespeare2.png
, Total.png
, Percent.png
,
Capita.png
. images. As always, please follow the same style guide lines as
CS1, which follows PEP8.