In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
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 = data[:,0]
ssc = data[:,1]
year, ssc
Out[3]:
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.
In [5]:
max_diff = 0 # This bit of code gives the largest difference of sunspot counts between any one year
for i in range(len(year)-1): # increment. Essentially, the largest slope on the graph.
delta = abs(ssc[i] - ssc[i+1])
if delta > max_diff:
max_diff = delta
max_diff
Out[5]:
In [6]:
f =plt.figure(figsize=(20,1.5))
plt.plot(year, ssc, 'g-')
plt.xlabel('Year')
plt.xlim(1700,2015)
plt.ylabel('Sunspot Count')
plt.grid(True)
In [7]:
assert True # leave for grading
Describe the choices you have made in building this visualization and how they make it effective.
I used code above to find the largest slope in the graph. Since this valus is more than 100, following the "steepest slope of 1" rule would make the plot aspect ratio 100:1, which makes the plot too short to even read. I tried to make it as visually appealing as I could. A ratio of 20:1.5 seems to work alright. I chose a green line just to be different from default. I chose to include the grid lines because it doesn't obscure the data much and makes it helpful when determining the y-axis values near the right end of the graph.
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:
In [8]:
f, ax = plt.subplots(4,1, figsize = (13,5), sharey = True)
plt.sca(ax[0])
plt.plot(year, ssc, 'r')
plt.xlim(1700,1800)
plt.sca(ax[1])
plt.plot(year, ssc, 'r')
plt.xlim(1800,1900)
plt.sca(ax[2])
plt.plot(year, ssc, 'r')
plt.xlim(1900,2000)
plt.sca(ax[3])
plt.plot(year, ssc, 'r')
plt.xlim(2000,2100)
plt.tight_layout()
In [9]:
assert True # leave for grading