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.loadtxt('yearssn.dat')
year,ssc = np.split(data,2,1)

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]:
fig, ax = plt.subplots(figsize=(15,4))
ax.plot(year,ssc)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
plt.xlabel('Year')
plt.ylabel('Number of Recorded Sunspots')
plt.title('Recorded Sunspots vs Year')
plt.xlim(1700,2015);



In [6]:
assert True # leave for grading

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

First I gave the visualization a title and labeled the axis in the standard location. I removed the top and right spine because they were unecessary and hurt my data to ink ratio. I changed the aspect ratio of the plot so that it was easier to see the trend.

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]:
fig, (ax1,ax2,ax3,ax4) = plt.subplots(4,1,figsize=(10,15),sharey=True)

ax1.plot(year[0:100], ssc[0:100])
ax2.plot(year[100:200], ssc[100:200])
ax3.plot(year[200:300], ssc[200:300])
ax4.plot(year[300:315], ssc[300:315])

ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax1.yaxis.set_ticks_position('left')
ax1.xaxis.set_ticks_position('bottom')

ax2.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.yaxis.set_ticks_position('left')
ax2.xaxis.set_ticks_position('bottom')

ax3.spines['right'].set_visible(False)
ax3.spines['top'].set_visible(False)
ax3.yaxis.set_ticks_position('left')
ax3.xaxis.set_ticks_position('bottom')

ax4.spines['right'].set_visible(False)
ax4.spines['top'].set_visible(False)
ax4.yaxis.set_ticks_position('left')
ax4.xaxis.set_ticks_position('bottom')

plt.xlim(2001,2014);

plt.sca(ax1)
plt.xlabel('Year')
plt.ylabel('Number of Recorded Sunspots')
plt.title('18th Century: Recorded Sunspots vs Year')

plt.sca(ax2)
plt.xlabel('Year')
plt.ylabel('Number of Recorded Sunspots')
plt.title('19th Century: Recorded Sunspots vs Year')

plt.sca(ax3)
plt.xlabel('Year')
plt.ylabel('Number of Recorded Sunspots')
plt.title('20th Century: Recorded Sunspots vs Year')

plt.sca(ax4)
plt.xlabel('Year')
plt.ylabel('Number of Recorded Sunspots')
plt.title('21st Century: Recorded Sunspots vs Year')


plt.tight_layout()


Splitting the data into four different plots is effective because the data is essentially the same for each century. But in the more separated form it is easy to see the oscillations happen about once every ten years.


In [8]:
assert True # leave for grading