Matplotlib Exercise 1

Imports


In [74]:
%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 [75]:
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 [76]:
data = np.loadtxt('yearssn.dat')
year = data[:,0]
ssc = data[:,1]

In [77]:
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 [78]:
plt.figure(figsize=(50,5))

plt.plot(year, ssc)
plt.grid(True)
plt.yticks([50,100,150,200], [50,100,150,200])
plt.xticks([1700,1750,1800,1850,1900,1950,2000], [1700,1750,1800,1850,1900,1950,2000])
plt.xlabel('Year')
plt.ylabel('Sun Spot Count')
plt.title('Sun Spot Counts per Year 1700-Now')


Out[78]:
<matplotlib.text.Text at 0x7f97a6703eb8>

In [79]:
assert True # leave for grading

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

YOUR ANSWER HERE

First I streched the graph way out horizontally to lessen the slope, making the data easier to read. I then labeled each axis and gave the graph a title, because not doing so is crazy talk. Then I altered what 'ticks' show up, as only certain values are important to what this graph is trying to show.

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 [103]:
year
year_1st=year[0:100]
year_2nd=year[101:200]
year_3rd=year[201:300]
year_4th=year[301:400]

ssc_1st=ssc[0:100]
ssc_2nd=ssc[101:200]
ssc_3rd=ssc[201:300]
ssc_4th=ssc[301:400]

plt.figure(figsize=(30,10))

plt.subplot(4,1,1)
plt.title('Sun Spot Counts per Year')
plt.plot(year_1st, ssc_1st)
plt.tight_layout()
plt.yticks([50,100,150,200], [50,100,150,200])
plt.ylabel('Sun Spot Count')

plt.subplot(4,1,2)
plt.plot(year_2nd, ssc_2nd)
plt.tight_layout()
plt.yticks([50,100,150,200], [50,100,150,200])
plt.ylabel('Sun Spot Count')

plt.subplot(4,1,3)
plt.plot(year_3rd, ssc_3rd)
plt.tight_layout()
plt.yticks([50,100,150,200], [50,100,150,200])
plt.ylabel('Sun Spot Count')

plt.subplot(4,1,4)
plt.plot(year_4th, ssc_4th)
plt.tight_layout()
plt.yticks([50,100,150,200], [50,100,150,200])
plt.xlabel('Year')
plt.ylabel('Sun Spot Count')


Out[103]:
<matplotlib.text.Text at 0x7f979be50320>

In [91]:
assert True # leave for grading

In [ ]: