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')
year=data[0:len(data),0]#gets the first term of every list in the array
ssc=data[0:len(data),1]#gets the 2nd term of each lsit

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 [6]:
f=plt.figure(figsize=(25,1))#extends the scale
plt.plot(year,ssc,'b')#the data to be ploted
plt.xlabel('Years')
plt.ylabel('Sunspots')
plt.title('Years v. Sunspots')#lables and title for clarity as with the ticks
plt.tick_params(axis='y', direction='inout', length=10)
plt.tick_params(axis='x', direction='inout', length=10)



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 an aspect ratio of 25:1 because that came the closest to the slope of one without rendering the graph too small to read. I increased the size of the tick marks to make it easier to read where the numbers were.

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 [40]:
data=np.loadtxt('yearssn.dat')#creates the range for the first subplot
cent1=data[0:100,0]
ss1=data[0:100,1]

In [17]:
data=np.loadtxt('yearssn.dat')#creates the range for the second subplot
cent2=data[100:200,0]
ss2=data[100:200,1]

In [19]:
data=np.loadtxt('yearssn.dat')#creates the range for the third subplot
cent3=data[200:300,0]
ss3=data[200:300,1]

In [21]:
data=np.loadtxt('yearssn.dat')#creates the range for the fouth subplot
cent4=data[300:400,0]
ss4=data[300:400,1]

In [50]:
plt.subplot(2,2,1,) 
plt.plot(cent1,ss1)#defines the first subplot location and data
plt.ylabel('Sunspot Count')

plt.subplot(2,2,2) #defines the second subplot location and data
plt.plot(cent2, ss2)

plt.subplot(2,2,3) #defines the third subplot location and data
plt.plot(cent3, ss3)
plt.ylabel('Sunspot Count')
plt.xlabel('Year')

plt.subplot(2,2,4) #defines the fourth subplot location and data
plt.plot(cent4,ss4)
plt.xlabel('Year')

plt.tight_layout()#makes things look nicer



In [16]:
assert True # leave for grading