Matplotlib Exercise 1

Imports


In [32]:
%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]:
#Btw, I asked Granger if I could turn this in during class and he said yes! Please don't dock my points!

data = np.loadtxt('yearssn.dat', dtype ='float',unpack=True)
years,ssc = data

In [4]:
# Another way
# np.ravel(data)
#even_index = np.arange(0,len(data),2)
# odd_index = np.arange(1,len(data),2)
# even_index
# years = data[even_index]
# ssc = data[odd_index]

In [5]:
assert len(years)==315
assert years.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 [33]:
plt.plot(years, ssc)
plt.xlabel('Years') #x labels
plt.ylabel('Sun Spot Count') #ylabels
plt.title('Sun Spot Count vs. Year') #Main Title
plt.grid(True) #plot grid
plt.box(False) #Taking out the box!!!
fig = plt.gcf()
# ????
fig.set_figwidth(100)



In [8]:
assert True # leave for grading

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

YOUR ANSWER HERE

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 [43]:
years_1 = years[years < 1800]
years_2 = years[years < 1900]
years_2 = years_2[years_2 >= 1800]
years_3 = years[years < 2000]
years_3 = years_3[years_3 >= 1900]
years_4 = years[years < 2100]
years_4 = years_4[years_4 >= 2000]
ssc_1 = ssc[years < 1800]
ssc_2 = ssc[years < 1900]
ssc_2 = ssc_2[years_2 >= 1800]
ssc_3 = ssc[years < 2000]
ssc_3 = ssc[years_3 >= 1900]
ssc_4 = ssc[years < 2100]
ssc_4 = ssc[years_4 >= 2000]

In [50]:
f, axarr = plt.subplots(4, sharex=True)
axarr[0].plot(years_1, ssc_1)
axarr[0].set_xlim([years_1.min(), years_1.max()])

axarr[1].plot(years_2, ssc_2)
axarr[1].set_xlim([years_2.min(), years_2.max()])

axarr[2].plot(years_3, ssc_3)
axarr[2].set_xlim([years_3.min(), years_3.max()])

axarr[3].plot(years_4, ssc_4)
axarr[3].set_xlim([years_4.min(), years_4.max()])


Out[50]:
(2000.5, 2014.5)

In [ ]:
assert True # leave for grading