Matplotlib Exercise 1

Imports


In [62]:
%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 [63]:
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 [64]:
data=np.loadtxt('yearssn.dat')
years=np.array(data[::1,0])
ssc=np.array(data[::1,1])
raise NotImplementedError()


---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-64-a4851a6a6e12> in <module>()
      2 years=np.array(data[::1,0])
      3 ssc=np.array(data[::1,1])
----> 4 raise NotImplementedError()

NotImplementedError: 

In [14]:
assert len(years)==315
assert years.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 [59]:
plt.figure(figsize=(20,1.5))
plt.plot(years,ssc,'r-')
plt.ylabel('sunspots')
plt.xlabel('year')
plt.title('Sunspots')
plt.xlim(1700,2015)
plt.ylim(0,200)


Out[59]:
(0, 200)

In [ ]:
assert True # leave for grading

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

I labeled the axes and gave it a title so it is clear what is being compared and what the graph represents. I stretched out the graph so the data was not cluttered and the data would be easier to see. The values are easy to accurately compare just by looking at 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:

  • 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 [66]:
plt.figure(figsize=(12,12))
for i in years:
    if i<1800:
        plt.subplot(4,1,1)
        plt.plot(years[:100:1],ssc[:100:1],'r-')
        plt.ylim(0,200)
        plt.title('Sunspots/Year')
    elif i>=1800 and i<1900:
        plt.subplot(4,1,2)
        plt.plot(years[101:200:1],ssc[101:200:1],'r-')
        plt.ylim(0,200)
    elif i>=1900 and i<2000:
        plt.subplot(4,1,3)
        plt.plot(years[201:300:1],ssc[201:300:1],'r-')
        plt.ylim(0,200)
    elif i>=2000:
        plt.subplot(4,1,4)
        plt.plot(years[301::1],ssc[301::1],'r-')
        plt.ylim(0,200)
        plt.xlim(2000,2100)
plt.tight_layout
raise NotImplementedError()


---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-66-d48ad9a37eba> in <module>()
     20         plt.xlim(2000,2100)
     21 plt.tight_layout
---> 22 raise NotImplementedError()

NotImplementedError: 

In [ ]:
assert True # leave for grading