In [14]:
%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 [15]:
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 [16]:
data = np.loadtxt('yearssn.dat')
#Creates two arrays, year is the first column of data and ssc is the second column of data
year = data[:,0]
ssc = data[:,1]
print (data)
In [17]:
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 [18]:
#Worked with Natasha Proctor
#This sets ax as the current axis (This allows me to manipulate spines and ticks)
ax = plt.gca()
#Plots the data
s = plt.plot(year,ssc)
#All of these following lines are for formatting.
plt.xlim(1700.5,2014.5)
plt.ylim(0,190.2)
plt.title('Sunspots Per Year')
plt.xlabel('Time')
plt.ylabel('Sunspots')
"""These lines utilize the fact I set ax to get current axis.
The spines are lines that compose the box around the graph.
So set_visible(False) makes whatever spine I chose to be invisible.
The tick calls get rid of the ticks on the spines that I have taken out.
set_aspect stretches the graph out so it can be closer to a max slope of 1. """
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.set_aspect(0.25)
plt.tight_layout()
In [19]:
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:
In [25]:
#Refer back to #1 for more explanation on the use of ax here
#plt.subplot works by taking parameters (rows of graphs, cols of graphs, which graph)
#So plt.subplo
Century_1 = year[0:100]
Ssc_1 = ssc[0:100]
plt.subplot(2,2,1)
ax1 = plt.gca()
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax1.get_xaxis().tick_bottom()
ax1.axes.get_yaxis().tick_left()
for label in ax1.xaxis.get_ticklabels()[::2] and ax1.yaxis.get_ticklabels()[::2]:
label.set_visible(False)
plt.plot(Century_1,Ssc_1)
plt.tight_layout()
In [ ]:
In [ ]:
assert True # leave for grading