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]
ssc = data[:,1]

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]:
plt.figure(figsize=(15, 1))
plt.plot(year, ssc, linewidth=2.0)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('#a2a7ff')
ax.spines['left'].set_color('#a2a7ff')
plt.xlim(1700,2016)
plt.xlabel("Year", fontsize = 14, color="#383838")
plt.ylabel("Number of Sunspots", fontsize = 14, color="#383838")
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.tick_params(axis='x', colors='#666666')
ax.tick_params(axis='y', colors='#666666')



In [7]:
assert True # leave for grading

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

With the smaller slope, it is easy to see the oscillating trend and the relatively consistent peaks and troughs. With the information more spread out, it is easier to compare the "Number of Sunspots" between centuries. I've used only necessary information to convey data and made the axis a gray color to limit my "data-ink ratio". I did make the choice to keep the axis titles darker just because it makes it easier to read. I also did not add a title for the graph, because the axis explain the information enough. A 'Number of Sunspots v Year' title would be redundant.

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 [8]:
plt.figure(figsize=(9,7))
plt.figure(1)
plt.subplot(411)
plt.plot(year, ssc, linewidth=2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('#a2a7ff')
ax.spines['left'].set_color('#a2a7ff')
plt.xlim(1700, 1800)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.tick_params(axis='x', colors='#666666')
ax.tick_params(axis='y', colors='#666666')

plt.subplot(412)
plt.plot(year, ssc, linewidth=2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('#a2a7ff')
ax.spines['left'].set_color('#a2a7ff')
plt.ylabel("Number of Sunspots", fontsize = 14, color="#383838")
plt.xlim(1800, 1900)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.tick_params(axis='x', colors='#666666')
ax.tick_params(axis='y', colors='#666666')

plt.subplot(413)
plt.plot(year, ssc, linewidth=2)
plt.xlim(1900, 2000)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('#a2a7ff')
ax.spines['left'].set_color('#a2a7ff')
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.tick_params(axis='x', colors='#666666')
ax.tick_params(axis='y', colors='#666666')



plt.subplot(414)
plt.plot(year, ssc, linewidth=2)
plt.xlim(2000,2100)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('#a2a7ff')
ax.spines['left'].set_color('#a2a7ff')
plt.xlabel("Year", fontsize = 14, color="#383838")
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.tick_params(axis='x', colors='#666666')
ax.tick_params(axis='y', colors='#666666')



plt.subplots_adjust(left=None, bottom=1, top=2, right=None, wspace=1, hspace=1.2)



In [8]:
assert True # leave for grading

In [9]:
# Note: on the 4th subplot, the graph is taking up unnecessary amount of space to convey the data between 2000-3000
# but I have made the subplots all identical in order to maintain graphical integrity.

In [ ]:
# used "Joe Kington"'s (from stackoverflow) method for setting axis tick and label colors
# used "timday"'s (from stackoverflow) method for hiding the top and right axis and ticks