Matplotlib Exercise 1

Imports


In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

Line plot of sunspot data

Download the .txt data for the "Yearly mean total sunspot number [1700 - now]" from the SILSO website. Upload the file to the same directory as this notebook.


In [3]:
import os
assert os.path.isfile('yearssn.dat')

Use np.loadtxt to read the data into a NumPy array called data. Then create two new 1d NumPy arrays named years and ssc that have the sequence of year and sunspot counts.


In [4]:
data = np.loadtxt('yearssn.dat')
year = data[:, 0]
ssc = data[:,1]
data


Out[4]:
array([[  1.70050000e+03,   5.00000000e+00],
       [  1.70150000e+03,   1.10000000e+01],
       [  1.70250000e+03,   1.60000000e+01],
       [  1.70350000e+03,   2.30000000e+01],
       [  1.70450000e+03,   3.60000000e+01],
       [  1.70550000e+03,   5.80000000e+01],
       [  1.70650000e+03,   2.90000000e+01],
       [  1.70750000e+03,   2.00000000e+01],
       [  1.70850000e+03,   1.00000000e+01],
       [  1.70950000e+03,   8.00000000e+00],
       [  1.71050000e+03,   3.00000000e+00],
       [  1.71150000e+03,   0.00000000e+00],
       [  1.71250000e+03,   0.00000000e+00],
       [  1.71350000e+03,   2.00000000e+00],
       [  1.71450000e+03,   1.10000000e+01],
       [  1.71550000e+03,   2.70000000e+01],
       [  1.71650000e+03,   4.70000000e+01],
       [  1.71750000e+03,   6.30000000e+01],
       [  1.71850000e+03,   6.00000000e+01],
       [  1.71950000e+03,   3.90000000e+01],
       [  1.72050000e+03,   2.80000000e+01],
       [  1.72150000e+03,   2.60000000e+01],
       [  1.72250000e+03,   2.20000000e+01],
       [  1.72350000e+03,   1.10000000e+01],
       [  1.72450000e+03,   2.10000000e+01],
       [  1.72550000e+03,   4.00000000e+01],
       [  1.72650000e+03,   7.80000000e+01],
       [  1.72750000e+03,   1.22000000e+02],
       [  1.72850000e+03,   1.03000000e+02],
       [  1.72950000e+03,   7.30000000e+01],
       [  1.73050000e+03,   4.70000000e+01],
       [  1.73150000e+03,   3.50000000e+01],
       [  1.73250000e+03,   1.10000000e+01],
       [  1.73350000e+03,   5.00000000e+00],
       [  1.73450000e+03,   1.60000000e+01],
       [  1.73550000e+03,   3.40000000e+01],
       [  1.73650000e+03,   7.00000000e+01],
       [  1.73750000e+03,   8.10000000e+01],
       [  1.73850000e+03,   1.11000000e+02],
       [  1.73950000e+03,   1.01000000e+02],
       [  1.74050000e+03,   7.30000000e+01],
       [  1.74150000e+03,   4.00000000e+01],
       [  1.74250000e+03,   2.00000000e+01],
       [  1.74350000e+03,   1.60000000e+01],
       [  1.74450000e+03,   5.00000000e+00],
       [  1.74550000e+03,   1.10000000e+01],
       [  1.74650000e+03,   2.20000000e+01],
       [  1.74750000e+03,   4.00000000e+01],
       [  1.74850000e+03,   6.00000000e+01],
       [  1.74950000e+03,   8.09000000e+01],
       [  1.75050000e+03,   8.34000000e+01],
       [  1.75150000e+03,   4.77000000e+01],
       [  1.75250000e+03,   4.78000000e+01],
       [  1.75350000e+03,   3.07000000e+01],
       [  1.75450000e+03,   1.22000000e+01],
       [  1.75550000e+03,   9.60000000e+00],
       [  1.75650000e+03,   1.02000000e+01],
       [  1.75750000e+03,   3.24000000e+01],
       [  1.75850000e+03,   4.76000000e+01],
       [  1.75950000e+03,   5.40000000e+01],
       [  1.76050000e+03,   6.29000000e+01],
       [  1.76150000e+03,   8.59000000e+01],
       [  1.76250000e+03,   6.12000000e+01],
       [  1.76350000e+03,   4.51000000e+01],
       [  1.76450000e+03,   3.64000000e+01],
       [  1.76550000e+03,   2.09000000e+01],
       [  1.76650000e+03,   1.14000000e+01],
       [  1.76750000e+03,   3.78000000e+01],
       [  1.76850000e+03,   6.98000000e+01],
       [  1.76950000e+03,   1.06100000e+02],
       [  1.77050000e+03,   1.00800000e+02],
       [  1.77150000e+03,   8.16000000e+01],
       [  1.77250000e+03,   6.65000000e+01],
       [  1.77350000e+03,   3.48000000e+01],
       [  1.77450000e+03,   3.06000000e+01],
       [  1.77550000e+03,   7.00000000e+00],
       [  1.77650000e+03,   1.98000000e+01],
       [  1.77750000e+03,   9.25000000e+01],
       [  1.77850000e+03,   1.54400000e+02],
       [  1.77950000e+03,   1.25900000e+02],
       [  1.78050000e+03,   8.48000000e+01],
       [  1.78150000e+03,   6.81000000e+01],
       [  1.78250000e+03,   3.85000000e+01],
       [  1.78350000e+03,   2.28000000e+01],
       [  1.78450000e+03,   1.02000000e+01],
       [  1.78550000e+03,   2.41000000e+01],
       [  1.78650000e+03,   8.29000000e+01],
       [  1.78750000e+03,   1.32000000e+02],
       [  1.78850000e+03,   1.30900000e+02],
       [  1.78950000e+03,   1.18100000e+02],
       [  1.79050000e+03,   8.99000000e+01],
       [  1.79150000e+03,   6.66000000e+01],
       [  1.79250000e+03,   6.00000000e+01],
       [  1.79350000e+03,   4.69000000e+01],
       [  1.79450000e+03,   4.10000000e+01],
       [  1.79550000e+03,   2.13000000e+01],
       [  1.79650000e+03,   1.60000000e+01],
       [  1.79750000e+03,   6.40000000e+00],
       [  1.79850000e+03,   4.10000000e+00],
       [  1.79950000e+03,   6.80000000e+00],
       [  1.80050000e+03,   1.45000000e+01],
       [  1.80150000e+03,   3.40000000e+01],
       [  1.80250000e+03,   4.50000000e+01],
       [  1.80350000e+03,   4.31000000e+01],
       [  1.80450000e+03,   4.75000000e+01],
       [  1.80550000e+03,   4.22000000e+01],
       [  1.80650000e+03,   2.81000000e+01],
       [  1.80750000e+03,   1.01000000e+01],
       [  1.80850000e+03,   8.10000000e+00],
       [  1.80950000e+03,   2.50000000e+00],
       [  1.81050000e+03,   0.00000000e+00],
       [  1.81150000e+03,   1.40000000e+00],
       [  1.81250000e+03,   5.00000000e+00],
       [  1.81350000e+03,   1.22000000e+01],
       [  1.81450000e+03,   1.39000000e+01],
       [  1.81550000e+03,   3.54000000e+01],
       [  1.81650000e+03,   4.58000000e+01],
       [  1.81750000e+03,   4.10000000e+01],
       [  1.81850000e+03,   3.01000000e+01],
       [  1.81950000e+03,   2.39000000e+01],
       [  1.82050000e+03,   1.56000000e+01],
       [  1.82150000e+03,   6.60000000e+00],
       [  1.82250000e+03,   4.00000000e+00],
       [  1.82350000e+03,   1.80000000e+00],
       [  1.82450000e+03,   8.50000000e+00],
       [  1.82550000e+03,   1.66000000e+01],
       [  1.82650000e+03,   3.63000000e+01],
       [  1.82750000e+03,   4.96000000e+01],
       [  1.82850000e+03,   6.42000000e+01],
       [  1.82950000e+03,   6.70000000e+01],
       [  1.83050000e+03,   7.09000000e+01],
       [  1.83150000e+03,   4.78000000e+01],
       [  1.83250000e+03,   2.75000000e+01],
       [  1.83350000e+03,   8.50000000e+00],
       [  1.83450000e+03,   1.32000000e+01],
       [  1.83550000e+03,   5.69000000e+01],
       [  1.83650000e+03,   1.21500000e+02],
       [  1.83750000e+03,   1.38300000e+02],
       [  1.83850000e+03,   1.03200000e+02],
       [  1.83950000e+03,   8.57000000e+01],
       [  1.84050000e+03,   6.46000000e+01],
       [  1.84150000e+03,   3.67000000e+01],
       [  1.84250000e+03,   2.42000000e+01],
       [  1.84350000e+03,   1.07000000e+01],
       [  1.84450000e+03,   1.50000000e+01],
       [  1.84550000e+03,   4.01000000e+01],
       [  1.84650000e+03,   6.15000000e+01],
       [  1.84750000e+03,   9.85000000e+01],
       [  1.84850000e+03,   1.24700000e+02],
       [  1.84950000e+03,   9.61000000e+01],
       [  1.85050000e+03,   6.65000000e+01],
       [  1.85150000e+03,   6.42000000e+01],
       [  1.85250000e+03,   5.41000000e+01],
       [  1.85350000e+03,   3.90000000e+01],
       [  1.85450000e+03,   2.05000000e+01],
       [  1.85550000e+03,   6.70000000e+00],
       [  1.85650000e+03,   4.30000000e+00],
       [  1.85750000e+03,   2.28000000e+01],
       [  1.85850000e+03,   5.50000000e+01],
       [  1.85950000e+03,   9.39000000e+01],
       [  1.86050000e+03,   9.59000000e+01],
       [  1.86150000e+03,   7.72000000e+01],
       [  1.86250000e+03,   5.90000000e+01],
       [  1.86350000e+03,   4.40000000e+01],
       [  1.86450000e+03,   4.70000000e+01],
       [  1.86550000e+03,   3.04000000e+01],
       [  1.86650000e+03,   1.62000000e+01],
       [  1.86750000e+03,   7.30000000e+00],
       [  1.86850000e+03,   3.76000000e+01],
       [  1.86950000e+03,   7.41000000e+01],
       [  1.87050000e+03,   1.39100000e+02],
       [  1.87150000e+03,   1.11100000e+02],
       [  1.87250000e+03,   1.01400000e+02],
       [  1.87350000e+03,   6.60000000e+01],
       [  1.87450000e+03,   4.46000000e+01],
       [  1.87550000e+03,   1.70000000e+01],
       [  1.87650000e+03,   1.13000000e+01],
       [  1.87750000e+03,   1.24000000e+01],
       [  1.87850000e+03,   3.40000000e+00],
       [  1.87950000e+03,   6.00000000e+00],
       [  1.88050000e+03,   3.22000000e+01],
       [  1.88150000e+03,   5.43000000e+01],
       [  1.88250000e+03,   5.94000000e+01],
       [  1.88350000e+03,   6.37000000e+01],
       [  1.88450000e+03,   6.35000000e+01],
       [  1.88550000e+03,   5.18000000e+01],
       [  1.88650000e+03,   2.55000000e+01],
       [  1.88750000e+03,   1.31000000e+01],
       [  1.88850000e+03,   6.70000000e+00],
       [  1.88950000e+03,   6.20000000e+00],
       [  1.89050000e+03,   7.10000000e+00],
       [  1.89150000e+03,   3.57000000e+01],
       [  1.89250000e+03,   7.30000000e+01],
       [  1.89350000e+03,   8.52000000e+01],
       [  1.89450000e+03,   7.80000000e+01],
       [  1.89550000e+03,   6.39000000e+01],
       [  1.89650000e+03,   4.17000000e+01],
       [  1.89750000e+03,   2.62000000e+01],
       [  1.89850000e+03,   2.67000000e+01],
       [  1.89950000e+03,   1.21000000e+01],
       [  1.90050000e+03,   9.40000000e+00],
       [  1.90150000e+03,   2.70000000e+00],
       [  1.90250000e+03,   5.10000000e+00],
       [  1.90350000e+03,   2.44000000e+01],
       [  1.90450000e+03,   4.21000000e+01],
       [  1.90550000e+03,   6.33000000e+01],
       [  1.90650000e+03,   5.41000000e+01],
       [  1.90750000e+03,   6.17000000e+01],
       [  1.90850000e+03,   4.85000000e+01],
       [  1.90950000e+03,   4.39000000e+01],
       [  1.91050000e+03,   1.85000000e+01],
       [  1.91150000e+03,   5.70000000e+00],
       [  1.91250000e+03,   3.60000000e+00],
       [  1.91350000e+03,   1.40000000e+00],
       [  1.91450000e+03,   9.60000000e+00],
       [  1.91550000e+03,   4.74000000e+01],
       [  1.91650000e+03,   5.70000000e+01],
       [  1.91750000e+03,   1.04200000e+02],
       [  1.91850000e+03,   8.08000000e+01],
       [  1.91950000e+03,   6.35000000e+01],
       [  1.92050000e+03,   3.76000000e+01],
       [  1.92150000e+03,   2.61000000e+01],
       [  1.92250000e+03,   1.42000000e+01],
       [  1.92350000e+03,   5.80000000e+00],
       [  1.92450000e+03,   1.67000000e+01],
       [  1.92550000e+03,   4.44000000e+01],
       [  1.92650000e+03,   6.39000000e+01],
       [  1.92750000e+03,   6.88000000e+01],
       [  1.92850000e+03,   7.78000000e+01],
       [  1.92950000e+03,   6.49000000e+01],
       [  1.93050000e+03,   3.56000000e+01],
       [  1.93150000e+03,   2.11000000e+01],
       [  1.93250000e+03,   1.11000000e+01],
       [  1.93350000e+03,   5.50000000e+00],
       [  1.93450000e+03,   8.70000000e+00],
       [  1.93550000e+03,   3.61000000e+01],
       [  1.93650000e+03,   7.97000000e+01],
       [  1.93750000e+03,   1.14400000e+02],
       [  1.93850000e+03,   1.09600000e+02],
       [  1.93950000e+03,   8.88000000e+01],
       [  1.94050000e+03,   6.78000000e+01],
       [  1.94150000e+03,   4.75000000e+01],
       [  1.94250000e+03,   3.04000000e+01],
       [  1.94350000e+03,   1.63000000e+01],
       [  1.94450000e+03,   9.70000000e+00],
       [  1.94550000e+03,   3.32000000e+01],
       [  1.94650000e+03,   9.26000000e+01],
       [  1.94750000e+03,   1.51600000e+02],
       [  1.94850000e+03,   1.36300000e+02],
       [  1.94950000e+03,   1.34700000e+02],
       [  1.95050000e+03,   8.39000000e+01],
       [  1.95150000e+03,   6.94000000e+01],
       [  1.95250000e+03,   3.15000000e+01],
       [  1.95350000e+03,   1.39000000e+01],
       [  1.95450000e+03,   4.40000000e+00],
       [  1.95550000e+03,   3.80000000e+01],
       [  1.95650000e+03,   1.41700000e+02],
       [  1.95750000e+03,   1.90200000e+02],
       [  1.95850000e+03,   1.84800000e+02],
       [  1.95950000e+03,   1.59000000e+02],
       [  1.96050000e+03,   1.12300000e+02],
       [  1.96150000e+03,   5.39000000e+01],
       [  1.96250000e+03,   3.75000000e+01],
       [  1.96350000e+03,   2.79000000e+01],
       [  1.96450000e+03,   1.02000000e+01],
       [  1.96550000e+03,   1.51000000e+01],
       [  1.96650000e+03,   4.70000000e+01],
       [  1.96750000e+03,   9.38000000e+01],
       [  1.96850000e+03,   1.05900000e+02],
       [  1.96950000e+03,   1.05500000e+02],
       [  1.97050000e+03,   1.04500000e+02],
       [  1.97150000e+03,   6.66000000e+01],
       [  1.97250000e+03,   6.89000000e+01],
       [  1.97350000e+03,   3.80000000e+01],
       [  1.97450000e+03,   3.45000000e+01],
       [  1.97550000e+03,   1.55000000e+01],
       [  1.97650000e+03,   1.26000000e+01],
       [  1.97750000e+03,   2.75000000e+01],
       [  1.97850000e+03,   9.25000000e+01],
       [  1.97950000e+03,   1.55400000e+02],
       [  1.98050000e+03,   1.54600000e+02],
       [  1.98150000e+03,   1.40500000e+02],
       [  1.98250000e+03,   1.15900000e+02],
       [  1.98350000e+03,   6.68000000e+01],
       [  1.98450000e+03,   4.57000000e+01],
       [  1.98550000e+03,   1.80000000e+01],
       [  1.98650000e+03,   1.34000000e+01],
       [  1.98750000e+03,   2.94000000e+01],
       [  1.98850000e+03,   1.00200000e+02],
       [  1.98950000e+03,   1.57600000e+02],
       [  1.99050000e+03,   1.42600000e+02],
       [  1.99150000e+03,   1.45700000e+02],
       [  1.99250000e+03,   9.43000000e+01],
       [  1.99350000e+03,   5.46000000e+01],
       [  1.99450000e+03,   2.99000000e+01],
       [  1.99550000e+03,   1.75000000e+01],
       [  1.99650000e+03,   8.60000000e+00],
       [  1.99750000e+03,   2.15000000e+01],
       [  1.99850000e+03,   6.43000000e+01],
       [  1.99950000e+03,   9.33000000e+01],
       [  2.00050000e+03,   1.19600000e+02],
       [  2.00150000e+03,   1.11000000e+02],
       [  2.00250000e+03,   1.04000000e+02],
       [  2.00350000e+03,   6.37000000e+01],
       [  2.00450000e+03,   4.04000000e+01],
       [  2.00550000e+03,   2.98000000e+01],
       [  2.00650000e+03,   1.52000000e+01],
       [  2.00750000e+03,   7.50000000e+00],
       [  2.00850000e+03,   2.90000000e+00],
       [  2.00950000e+03,   3.10000000e+00],
       [  2.01050000e+03,   1.65000000e+01],
       [  2.01150000e+03,   5.57000000e+01],
       [  2.01250000e+03,   5.77000000e+01],
       [  2.01350000e+03,   6.49000000e+01],
       [  2.01450000e+03,   7.89000000e+01]])

In [17]:
assert len(year)==315
assert year.dtype==np.dtype(float)
assert len(ssc)==315
assert ssc.dtype==np.dtype(float)

Make a line plot showing the sunspot count as a function of year.

  • Customize your plot to follow Tufte's principles of visualizations.
  • Adjust the aspect ratio/size so that the steepest slope in your plot is approximately 1.
  • Customize the box, grid, spines and ticks to match the requirements of this data.

In [48]:
plt.figure(figsize=(20,6))
plt.plot(year, ssc)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.xlim(1700, 2015)
plt.grid(True)



In [ ]:
assert True # leave for grading

Describe the choices you have made in building this visualization and how they make it effective.

Since there are so many highs and lows in this graph, I made the graph very wide so that these highs and lows are spread out and easier to tell apart. I turned on the gridlines so that it would be easier to see where the values on the very right of the graph lie with respect to the y-axis. I left the box turned on to make it easy to distinguish the graph from the rest of the page, since there is so much whitespace the two tend to blend together. The blue line sets the graph line apart from the box and gridlines, but is also a nice, solid color so it is easily readable.

Now make 4 subplots, one for each century in the data set. This approach works well for this dataset as it allows you to maintain mild slopes while limiting the overall width of the visualization. Perform similar customizations as above:

  • Customize your plot to follow Tufte's principles of visualizations.
  • Adjust the aspect ratio/size so that the steepest slope in your plot is approximately 1.
  • Customize the box, grid, spines and ticks to match the requirements of this data.

In [46]:
plt.figure(figsize=(20,6))

plt.subplot(2,2,1)
plt.plot(year, ssc)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.xlim(1700, 1800)
plt.grid(True)

plt.subplot(2,2,2)
plt.plot(year, ssc)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.xlim(1800, 1900)
plt.grid(True)

plt.subplot(2,2,3)
plt.plot(year, ssc)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.xlim(1900, 2000)
plt.grid(True)

plt.subplot(2,2,4)
plt.plot(year, ssc)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.xlim(2000, 2100)
plt.grid(True)



In [ ]:
assert True # leave for grading