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]:
data=np.loadtxt('yearssn.dat') #loads the file from SILSo into an array
year=np.array(data[:,0]) #takes each element from the data and puts into 
ssc=np.array(data[:,1]) #a 1d array

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 [13]:
plt.figure(figsize=(15,1))
plt.plot(year,ssc, "b")
ax=plt.gca()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()

plt.xlabel("Year")
plt.ylabel("Sunspot Count")
plt.title("SILSO Sunspots vs Year")
plt.xlim(right=2020)
plt.xticks(range(1700,2025,25))





#add more detail and still add the scalling


Out[13]:
([<matplotlib.axis.XTick at 0x7f01f6c23358>,
  <matplotlib.axis.XTick at 0x7f01f6bd5358>,
  <matplotlib.axis.XTick at 0x7f01f6bddbe0>,
  <matplotlib.axis.XTick at 0x7f01f6bb4f28>,
  <matplotlib.axis.XTick at 0x7f01f6bb9940>,
  <matplotlib.axis.XTick at 0x7f01f6b3e358>,
  <matplotlib.axis.XTick at 0x7f01f6b3ed30>,
  <matplotlib.axis.XTick at 0x7f01f6b41748>,
  <matplotlib.axis.XTick at 0x7f01f6b46160>,
  <matplotlib.axis.XTick at 0x7f01f6b46b38>,
  <matplotlib.axis.XTick at 0x7f01f6b49550>,
  <matplotlib.axis.XTick at 0x7f01f6b49f28>,
  <matplotlib.axis.XTick at 0x7f01f6b4d940>],
 <a list of 13 Text xticklabel objects>)

In [57]:
assert True # leave for grading

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

I labeled both axes and gaves the graph a title allowing for the viewer to understand the data they are presented. I also removed the box as it was not useful to interpreting the information and providing visual clutter. I formatted the ticks along the x axis to increment every 25 years.

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 [15]:
year1=year[0:100]
ssc1=ssc[0:100]
year2=year[100:200]
ssc2=ssc[100:200]
year3=year[200:300]
ssc3=ssc[200:300]
year4=year[300:315]
ssc4=ssc[300:315]

f=plt.figure(figsize=(12,8))

plt.subplot(4,1,1)
plt.plot(year1,ssc1)
plt.title('18th Century')
plt.yticks([0,80,160])
plt.ylabel('Sunspots')
ax=plt.gca()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()

plt.subplot(4,1,2)
plt.plot(year2,ssc2)
plt.title('19th Century')
plt.yticks([0,70,140])
plt.ylabel('Sunspots')
ax=plt.gca()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()

plt.subplot(4,1,3)
plt.plot(year3,ssc3)
plt.title("20th Century")
plt.yticks([0,100,200])
plt.ylabel('Sunspots')
ax=plt.gca()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()

plt.subplot(4,1,4)
plt.plot(year4,ssc4)
plt.xlabel('Year')
plt.title('21st Century')
plt.yticks([0,60,120])
plt.ylabel('Sunspots')
ax=plt.gca()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()

plt.tight_layout()



In [ ]:
assert True # leave for grading