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]:
# YOUR CODE HERE
f = np.loadtxt('yearssn.dat')
data = np.array(f)
split = np.hsplit(data, 2)
flat = np.ravel(split)
year = flat[0:315:1]
ssc = flat[315::]

In [5]:
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 [30]:
# YOUR CODE HERE
f = plt.figure(figsize=(27,6))
fig = plt.plot(year, ssc)
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.xlabel('Years')
plt.ylabel('Sun Spot Count')
plt.title('Sunspots over the years')
plt.xlim(1700, 2020)
plt.tick_params(axis='y', direction='out', length=10)
plt.grid()



In [9]:
assert True # leave for grading

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

YOUR ANSWER HERE: I lowered the aspect ratio so that the lines appear less steep. I also removed the top and right hand spines and tick marks to make it less cluttered looking on the sides, and added grid lines to help follow the data.

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 [29]:
# YOUR CODE HERE
f = plt.figure(figsize=(15,9))


plt.subplot(2, 2, 1)
plt.plot(year[0:100], ssc[0:100])
plt.xlabel('Years')
plt.ylabel('Sun Spot Count')
plt.title('Sunspots over the 1700\'s')
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.grid()

plt.subplot(2, 2, 2)
plt.plot(year[101:200], ssc[101:200])
plt.xlabel('Years')
plt.ylabel('Sun Spot Count')
plt.title('Sunspots over the 1800\'s')
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.grid()

plt.subplot(2, 2, 3)
plt.plot(year[201:300], ssc[201:300])
plt.xlabel('Years')
plt.ylabel('Sun Spot Count')
plt.title('Sunspots over the 1900\'s')
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.grid()

plt.subplot(2, 2, 4)
plt.plot(year[300:320], ssc[300:320])
plt.xlabel('Years')
plt.ylabel('Sun Spot Count')
plt.title('Sunspots til now')
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()

plt.tight_layout()
plt.grid()



In [ ]:
assert True # leave for grading