# Read seasonal snowfalls from National Oceanic and Atmospheric Administration import urllib html = urllib.urlopen("http://www.erh.noaa.gov/box/climate/bossnw.shtml").read() delim = "1891-92" data1 = delim + html.split(delim)[1] data2 = data1.split("\n\n")[0] a = data2.split('\n') b = [ (x.split()[0], x.split()[-1]) for x in a ] c = [ (float(x[-1]), x[0]) for x in b ] d = sorted( c, key=lambda x: x[0] ) d.reverse() # Largest snowfalls first print( "Ten largest snowfalls:\n" + '\n'.join( [str((x[1], x[0])) for x in d[:10]] ) ) #======================================= # And now graph c (snowfall in chronological order) import matplotlib.pyplot matplotlib.pyplot.xlabel("Year") # The list c is: [ (46.8, '1891-92'), ... ] # Pick out labels for [0:len(c)+1:10] (from 0 to len(c), for every 10th elt) matplotlib.pyplot.xticks( range(0, len(c), 10), [x[1] for x in c][0:len(c)+1:10] ) locs, labels = matplotlib.pyplot.xticks() # Get the labels field for x-axix matplotlib.pyplot.setp(labels, rotation=45) # Rotate the labels 45 degrees matplotlib.pyplot.axis(xmin=0) matplotlib.pyplot.axis(xmax=len(c)+1) matplotlib.pyplot.ylabel("Snowfall (in.)") # This must be done after adjusting the x- and y-axes. matplotlib.pyplot.tight_layout(w_pad=6, h_pad=6) # padding in width, height matplotlib.pyplot.scatter(range(len(c)), [x[0] for x in c]) # scatter plot matplotlib.pyplot.title("Seasonal snowfall in Boston") matplotlib.pyplot.show()