# Matplotlib Exercise 1

## Imports

``````

In [45]:

%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 [46]:

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

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

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

In [48]:

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.

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

plt.plot(years, ssc)
plt.ylim(0, 200)
plt.xlim(right = 2025)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.title('Sunspot Count Since 1700')
axis = plt.gca()
axis.spines['top'].set_visible(False)
axis.spines['right'].set_visible(False)
axis.get_xaxis().tick_bottom()
axis.get_yaxis().tick_left()

plt.tight_layout()

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

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

In [50]:

assert True # leave for grading

``````

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

I added a title and axis labels to give context. I also changed the x axis limit so save space. Then I took away the right and top boarders and tick marks because they served no purpose on this 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:

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

plt.subplots(2, 2, sharex=True, sharey=True, figsize=(6,6))

plt.subplot(2,2,1)
plt.plot(years, ssc)
plt.ylim(0,160)
plt.xlim(1700,1800)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.title('Sunspot Count 1700-1800')
axis = plt.gca()
axis.spines['top'].set_visible(False)
axis.spines['right'].set_visible(False)
axis.get_xaxis().tick_bottom()
axis.get_yaxis().tick_left()
axis.set_aspect(0.5)
plt.tight_layout()

plt.subplot(2,2,2)
plt.plot(years, ssc)
plt.ylim(0,150)
plt.xlim(1800,1900)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.title('Sunspot Count 1800-1900')
axis = plt.gca()
axis.spines['top'].set_visible(False)
axis.spines['right'].set_visible(False)
axis.get_xaxis().tick_bottom()
axis.get_yaxis().tick_left()
axis.set_aspect(0.5)
plt.tight_layout()

plt.subplot(2,2,3)
plt.plot(years, ssc)
plt.ylim()
plt.xlim(1900,2000)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.title('Sunspot Count 1900-2000')
axis = plt.gca()
axis.spines['top'].set_visible(False)
axis.spines['right'].set_visible(False)
axis.get_xaxis().tick_bottom()
axis.get_yaxis().tick_left()
axis.set_aspect(0.5)
plt.tight_layout()

plt.subplot(2,2,4)
plt.plot(years, ssc)
plt.ylim(0,125)
plt.xlim(2000,2015)
plt.xlabel('Year')
plt.ylabel('Sunspot Count')
plt.title('Sunspot Count 2000-2015')
axis = plt.gca()
axis.spines['top'].set_visible(False)
axis.spines['right'].set_visible(False)
axis.get_xaxis().tick_bottom()
axis.get_yaxis().tick_left()
plt.xticks([2000,2005,2010,2015])
plt.tight_layout()

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

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

In [ ]:

assert True # leave for grading

``````