# 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 [2]:

import os

``````

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 [3]:

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

``````
``````

In [4]:

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.

• 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 [39]:

f = plt.figure(figsize = (20, 2))
plt.plot(years, ssc, "b-")
plt.box(False)
plt.xticks(np.linspace(1700, 2015, 5, dtype = int))
plt.yticks(np.linspace(0, 150, 3, dtype = int))
plt.xlabel("Year")
plt.ylabel("# of Sunspots")
plt.title("# of Sunspots Per Year")

``````
``````

Out[39]:

<matplotlib.text.Text at 0x7fc12f79e6a0>

``````
``````

In [ ]:

assert True # leave for grading

``````

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

I removed the box and grid because nobody cares exactly how many sunspots there were, all that matters is the oscilitory behavior. This is plainly shown here. The ticks give a good measure of time, it is easy to estimate the number of years between peaks. As well, the y axis shows the order of magnitude well.

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:

• 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]:

f = plt.figure(figsize = (15,8))
plt.subplot(4,1,1)
plt.plot(years[:100], ssc[:100], "b-")
plt.box(False)
plt.xticks(np.linspace(1700, 1800, 5, dtype = int))
plt.yticks(np.linspace(0, 150, 3, dtype = int))
plt.xlabel("Year")
plt.ylabel("# of Sunspots")
plt.title("# of Sunspots Per Year from 1700-1800")

plt.subplot(4,1,2)
plt.plot(years[100:200], ssc[100:200], "b-")
plt.box(False)
plt.xticks(np.linspace(1800, 1900, 5, dtype = int))
plt.yticks(np.linspace(0, 150, 3, dtype = int))
plt.xlabel("Year")
plt.ylabel("# of Sunspots")
plt.title("# of Sunspots Per Year from 1800-1900")

plt.subplot(4,1,3)
plt.plot(years[200:300], ssc[200:300], "b-")
plt.box(False)
plt.xticks(np.linspace(1900, 2000, 5, dtype = int))
plt.yticks(np.linspace(0, 150, 3, dtype = int))
plt.xlabel("Year")
plt.ylabel("# of Sunspots")
plt.title("# of Sunspots Per Year from 1900-2000")

plt.subplot(4,1,4)
plt.plot(years[300:], ssc[300:], "b-")
plt.box(False)
plt.xticks(np.linspace(2000, 2100, 5, dtype = int))
plt.yticks(np.linspace(0, 150, 3, dtype = int))
plt.xlabel("Year")
plt.ylabel("# of Sunspots")
plt.title("# of Sunspots Per Year from 2000-2015")

plt.tight_layout()

``````
``````

``````
``````

In [ ]:

assert True # leave for grading

``````