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 [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')


year=data[:,0]
ssc=data[:,1]

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 [16]:
plt.figure(figsize=(30,1))
plt.plot(year,ssc)
plt.xlabel('year')
plt.ylabel('ssc')
plt.title('Sunspots vs years')
plt.grid(True)
plt.box(False)
plt.xlim(right=2020)


Out[16]:
(1700.0, 2020)

In [118]:
assert True # leave for grading

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

-Made fig size larger because of how squished the data points looked. -Added axes titles ( so we know what we are looking at) -Made gridlines to make it easier to pinpoint where the points are relative to scale -Deleted the spines of the box, they are not needed

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 [119]:
plt.figure(figsize=(20,5))

plt.subplot(4,1,1)
plt.plot(year,ssc)
plt.xlim(1700,1800)
plt.ylabel('ssc')
plt.yticks([0,100,200],[0,100,200])
plt.box(False)
plt.tight_layout()


plt.subplot(4,1,2)
plt.plot(year,ssc)
plt.xlim(1801,1900)
plt.ylabel('ssc')
plt.yticks([0,100,200],[0,100,200])
plt.box(False)

plt.subplot(4,1,3)
plt.plot(year,ssc)
plt.xlim(1901,2000)
plt.ylabel('ssc')
plt.yticks([0,100,200],[0,100,200])
plt.box(False)

plt.subplot(4,1,4)
plt.plot(year,ssc)
plt.xlim(2000,2015)
plt.yticks([0,100,200],[0,100,200])
plt.ylabel('ssc')
plt.xlabel('year')

plt.box(False)


plt.tight_layout()



In [120]:
assert True # leave for grading

In [ ]: