Matplotlib Exercise 1

Imports


In [4]:
%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 [5]:
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 [6]:
# YOUR CODE HERE
#raise NotImplementedError()
data = np.loadtxt('yearssn.dat')
year = np.ones(len(data))
ssc = np.ones(len(data))
#makes two new arrays, one for each array in data
for i in range(0, len(data)):
    ssc[i] = data[i][1]
    year[i] = data[i][0]

In [7]:
assert len(year)==315
assert year.dtype==np.dtype(float)
assert len(ssc)==315
assert ssc.dtype==np.dtype(float)

In [46]:
#origonally used this to find max slope so I could scale for slope of 1
#turned out I didnt need it.
""""def average(data,num):
    avex=[]
    avey=[]
    x=data[0]
    y=data[1]
    for i in range(len(x)-(num)):
        avex.append(sum(x[i:i+num])/num)
        avey.append(sum(y[i:i+num])/num)
    return avex,avey

x_ave,y_ave = average((year,ssc), 4)

def max_slope(x,y):
    s=[]
    for i in range(0, len(x)-1):
        s.append((y[i+1]-y[i])/(x[i+1]-x[i]))
    return max(s) 
max_slope(x_ave, y_ave)"""


Out[46]:
'"def average(data,num):\n    avex=[]\n    avey=[]\n    x=data[0]\n    y=data[1]\n    for i in range(len(x)-(num)):\n        avex.append(sum(x[i:i+num])/num)\n        avey.append(sum(y[i:i+num])/num)\n    return avex,avey\n\nx_ave,y_ave = average((year,ssc), 4)\n\ndef max_slope(x,y):\n    s=[]\n    for i in range(0, len(x)-1):\n        s.append((y[i+1]-y[i])/(x[i+1]-x[i]))\n    return max(s) \nmax_slope(x_ave, y_ave)'

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 [24]:
# YOUR CODE HERE
#raise NotImplementedError()
f= plt.figure(figsize=(15,1))
plt.plot(year, ssc)
plt.xlabel('Year')
plt.ylabel('Number of Sunspots')
plt.title('Sunspot Count')
plt.xlim(1700,2015)
plt.ylim(0,191)
plt.box(False)
a=range(192)
b= range(1700,2016)
plt.yticks(a[::50])
plt.show()



In [10]:
assert True # leave for grading

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

YOUR ANSWER HERE

Chose to not have box or gridlines in order to maximize data to ink ratio, this looks cleaner and makes the data eaiser to read. Labled the axes this way because gave acurate yet simple discription of the data plotted. I chose the scale in order to make max slope closer to 1. The y-axis and x-axis ticks were chosen to be able to show the data scope without completely over crowding the axes.

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 [45]:
# YOUR CODE HERE
#raise NotImplementedError()
a= range(192)
f = plt.figure(figsize=(15,6))
plt.subplot(4,1,1)
plt.yticks(a[::50])
plt.title('Sunspot Count')
plt.plot(year[0:100],ssc[0:100])
plt.box(False)

plt.subplot(4,1,2)
plt.plot(year[100:200], ssc[100:200])
plt.yticks(a[::50])
plt.box(False)

plt.subplot(4,1,3)
plt.plot(year[200:300],ssc[200:300])
plt.yticks(a[::50])
plt.ylabel('Number of Sunspots')
plt.box(False)


plt.subplot(4,1,4)
plt.plot(year[300:400], ssc[300:400])
plt.yticks(a[::50])
plt.xlim(2000,2100)
plt.xlabel('Year')
plt.box(False)

plt.tight_layout()



In [43]:
assert True # leave for grading