Matplotlib Exercise 1

Imports


In [1]:
%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 [2]:
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 [3]:
data=np.array(np.loadtxt('yearssn.dat'))
year=np.array(data[0::1,0::2]) #splits the data into two arrays
ssc=np.array(data[0::1,1::2])

In [4]:
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 [5]:
y=plt.figure(figsize=(15,1))
plt.plot(year, ssc) #makes a plot of the two arrays
plt.xlabel('Year')
plt.ylabel('SSC')
plt.xlim(right=2016)

plt.title('Sunspot Count vs. Year')


Out[5]:
<matplotlib.text.Text at 0x7fa1df0cdeb8>

In [6]:
assert True # leave for grading

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

So far, I have just labeled the axes, gave the visualizaton a title, set the limits of the x-axis, and increased the size of the visualization.

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 [7]:
#referenced http://goo.gl/lle8W6
#for ticks and spines
f,((graph0,graph2),(graph1,graph3))=plt.subplots(nrows=2,ncols=2,figsize=(12,6),sharey=True)
graph0.plot(year[0:100],ssc[0:100]) # makes subpots of the different centuries of data
graph2.plot(year[200:300],ssc[200:300])
graph1.plot(year[100:200],ssc[100:200])
graph3.plot(year[300:400],ssc[300:400])
graph3.set_xlim(right=2100)

graph0.spines['right'].set_visible(False) # formatting all the right and top axes of the data to reduce ink "use"
graph0.spines['top'].set_visible(False)   # as well as making titles and labels for the axes
graph0.yaxis.set_ticks_position('left')
graph0.xaxis.set_ticks_position('bottom')
graph0.set_ylabel('SSC')
graph0.set_title('1700-1800')
graph1.spines['right'].set_visible(False)
graph1.spines['top'].set_visible(False)
graph1.yaxis.set_ticks_position('left')
graph1.xaxis.set_ticks_position('bottom')
graph1.set_title('1800-1900')
graph2.spines['right'].set_visible(False)
graph2.spines['top'].set_visible(False)
graph2.yaxis.set_ticks_position('left')
graph2.xaxis.set_ticks_position('bottom')
graph2.set_title('1900-2000')
graph1.set_xlabel('Years in Century Blocks')
graph1.set_ylabel('SSC')
graph3.spines['right'].set_visible(False)
graph3.spines['top'].set_visible(False)
graph3.yaxis.set_ticks_position('left')
graph3.xaxis.set_ticks_position('bottom')
graph3.set_xlabel('Years in Century Blocks')
graph3.set_title('2000-2100')

plt.tight_layout()



In [8]:
assert True # leave for grading