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
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 [3]:
data = np.loadtxt('yearssn.dat')
year = data[:,0]
ssc = data[:,1]
year, ssc


Out[3]:
(array([ 1700.5,  1701.5,  1702.5,  1703.5,  1704.5,  1705.5,  1706.5,
         1707.5,  1708.5,  1709.5,  1710.5,  1711.5,  1712.5,  1713.5,
         1714.5,  1715.5,  1716.5,  1717.5,  1718.5,  1719.5,  1720.5,
         1721.5,  1722.5,  1723.5,  1724.5,  1725.5,  1726.5,  1727.5,
         1728.5,  1729.5,  1730.5,  1731.5,  1732.5,  1733.5,  1734.5,
         1735.5,  1736.5,  1737.5,  1738.5,  1739.5,  1740.5,  1741.5,
         1742.5,  1743.5,  1744.5,  1745.5,  1746.5,  1747.5,  1748.5,
         1749.5,  1750.5,  1751.5,  1752.5,  1753.5,  1754.5,  1755.5,
         1756.5,  1757.5,  1758.5,  1759.5,  1760.5,  1761.5,  1762.5,
         1763.5,  1764.5,  1765.5,  1766.5,  1767.5,  1768.5,  1769.5,
         1770.5,  1771.5,  1772.5,  1773.5,  1774.5,  1775.5,  1776.5,
         1777.5,  1778.5,  1779.5,  1780.5,  1781.5,  1782.5,  1783.5,
         1784.5,  1785.5,  1786.5,  1787.5,  1788.5,  1789.5,  1790.5,
         1791.5,  1792.5,  1793.5,  1794.5,  1795.5,  1796.5,  1797.5,
         1798.5,  1799.5,  1800.5,  1801.5,  1802.5,  1803.5,  1804.5,
         1805.5,  1806.5,  1807.5,  1808.5,  1809.5,  1810.5,  1811.5,
         1812.5,  1813.5,  1814.5,  1815.5,  1816.5,  1817.5,  1818.5,
         1819.5,  1820.5,  1821.5,  1822.5,  1823.5,  1824.5,  1825.5,
         1826.5,  1827.5,  1828.5,  1829.5,  1830.5,  1831.5,  1832.5,
         1833.5,  1834.5,  1835.5,  1836.5,  1837.5,  1838.5,  1839.5,
         1840.5,  1841.5,  1842.5,  1843.5,  1844.5,  1845.5,  1846.5,
         1847.5,  1848.5,  1849.5,  1850.5,  1851.5,  1852.5,  1853.5,
         1854.5,  1855.5,  1856.5,  1857.5,  1858.5,  1859.5,  1860.5,
         1861.5,  1862.5,  1863.5,  1864.5,  1865.5,  1866.5,  1867.5,
         1868.5,  1869.5,  1870.5,  1871.5,  1872.5,  1873.5,  1874.5,
         1875.5,  1876.5,  1877.5,  1878.5,  1879.5,  1880.5,  1881.5,
         1882.5,  1883.5,  1884.5,  1885.5,  1886.5,  1887.5,  1888.5,
         1889.5,  1890.5,  1891.5,  1892.5,  1893.5,  1894.5,  1895.5,
         1896.5,  1897.5,  1898.5,  1899.5,  1900.5,  1901.5,  1902.5,
         1903.5,  1904.5,  1905.5,  1906.5,  1907.5,  1908.5,  1909.5,
         1910.5,  1911.5,  1912.5,  1913.5,  1914.5,  1915.5,  1916.5,
         1917.5,  1918.5,  1919.5,  1920.5,  1921.5,  1922.5,  1923.5,
         1924.5,  1925.5,  1926.5,  1927.5,  1928.5,  1929.5,  1930.5,
         1931.5,  1932.5,  1933.5,  1934.5,  1935.5,  1936.5,  1937.5,
         1938.5,  1939.5,  1940.5,  1941.5,  1942.5,  1943.5,  1944.5,
         1945.5,  1946.5,  1947.5,  1948.5,  1949.5,  1950.5,  1951.5,
         1952.5,  1953.5,  1954.5,  1955.5,  1956.5,  1957.5,  1958.5,
         1959.5,  1960.5,  1961.5,  1962.5,  1963.5,  1964.5,  1965.5,
         1966.5,  1967.5,  1968.5,  1969.5,  1970.5,  1971.5,  1972.5,
         1973.5,  1974.5,  1975.5,  1976.5,  1977.5,  1978.5,  1979.5,
         1980.5,  1981.5,  1982.5,  1983.5,  1984.5,  1985.5,  1986.5,
         1987.5,  1988.5,  1989.5,  1990.5,  1991.5,  1992.5,  1993.5,
         1994.5,  1995.5,  1996.5,  1997.5,  1998.5,  1999.5,  2000.5,
         2001.5,  2002.5,  2003.5,  2004.5,  2005.5,  2006.5,  2007.5,
         2008.5,  2009.5,  2010.5,  2011.5,  2012.5,  2013.5,  2014.5]),
 array([   5. ,   11. ,   16. ,   23. ,   36. ,   58. ,   29. ,   20. ,
          10. ,    8. ,    3. ,    0. ,    0. ,    2. ,   11. ,   27. ,
          47. ,   63. ,   60. ,   39. ,   28. ,   26. ,   22. ,   11. ,
          21. ,   40. ,   78. ,  122. ,  103. ,   73. ,   47. ,   35. ,
          11. ,    5. ,   16. ,   34. ,   70. ,   81. ,  111. ,  101. ,
          73. ,   40. ,   20. ,   16. ,    5. ,   11. ,   22. ,   40. ,
          60. ,   80.9,   83.4,   47.7,   47.8,   30.7,   12.2,    9.6,
          10.2,   32.4,   47.6,   54. ,   62.9,   85.9,   61.2,   45.1,
          36.4,   20.9,   11.4,   37.8,   69.8,  106.1,  100.8,   81.6,
          66.5,   34.8,   30.6,    7. ,   19.8,   92.5,  154.4,  125.9,
          84.8,   68.1,   38.5,   22.8,   10.2,   24.1,   82.9,  132. ,
         130.9,  118.1,   89.9,   66.6,   60. ,   46.9,   41. ,   21.3,
          16. ,    6.4,    4.1,    6.8,   14.5,   34. ,   45. ,   43.1,
          47.5,   42.2,   28.1,   10.1,    8.1,    2.5,    0. ,    1.4,
           5. ,   12.2,   13.9,   35.4,   45.8,   41. ,   30.1,   23.9,
          15.6,    6.6,    4. ,    1.8,    8.5,   16.6,   36.3,   49.6,
          64.2,   67. ,   70.9,   47.8,   27.5,    8.5,   13.2,   56.9,
         121.5,  138.3,  103.2,   85.7,   64.6,   36.7,   24.2,   10.7,
          15. ,   40.1,   61.5,   98.5,  124.7,   96.1,   66.5,   64.2,
          54.1,   39. ,   20.5,    6.7,    4.3,   22.8,   55. ,   93.9,
          95.9,   77.2,   59. ,   44. ,   47. ,   30.4,   16.2,    7.3,
          37.6,   74.1,  139.1,  111.1,  101.4,   66. ,   44.6,   17. ,
          11.3,   12.4,    3.4,    6. ,   32.2,   54.3,   59.4,   63.7,
          63.5,   51.8,   25.5,   13.1,    6.7,    6.2,    7.1,   35.7,
          73. ,   85.2,   78. ,   63.9,   41.7,   26.2,   26.7,   12.1,
           9.4,    2.7,    5.1,   24.4,   42.1,   63.3,   54.1,   61.7,
          48.5,   43.9,   18.5,    5.7,    3.6,    1.4,    9.6,   47.4,
          57. ,  104.2,   80.8,   63.5,   37.6,   26.1,   14.2,    5.8,
          16.7,   44.4,   63.9,   68.8,   77.8,   64.9,   35.6,   21.1,
          11.1,    5.5,    8.7,   36.1,   79.7,  114.4,  109.6,   88.8,
          67.8,   47.5,   30.4,   16.3,    9.7,   33.2,   92.6,  151.6,
         136.3,  134.7,   83.9,   69.4,   31.5,   13.9,    4.4,   38. ,
         141.7,  190.2,  184.8,  159. ,  112.3,   53.9,   37.5,   27.9,
          10.2,   15.1,   47. ,   93.8,  105.9,  105.5,  104.5,   66.6,
          68.9,   38. ,   34.5,   15.5,   12.6,   27.5,   92.5,  155.4,
         154.6,  140.5,  115.9,   66.8,   45.7,   18. ,   13.4,   29.4,
         100.2,  157.6,  142.6,  145.7,   94.3,   54.6,   29.9,   17.5,
           8.6,   21.5,   64.3,   93.3,  119.6,  111. ,  104. ,   63.7,
          40.4,   29.8,   15.2,    7.5,    2.9,    3.1,   16.5,   55.7,
          57.7,   64.9,   78.9]))

In [4]:
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 [5]:
max_diff = 0                        # This bit of code gives the largest difference of sunspot counts between any one year
for i in range(len(year)-1):        # increment. Essentially, the largest slope on the graph.
    delta = abs(ssc[i] - ssc[i+1])
    if delta > max_diff:
        max_diff = delta
max_diff


Out[5]:
103.69999999999999

In [6]:
f =plt.figure(figsize=(20,1.5))

plt.plot(year, ssc, 'g-')
plt.xlabel('Year')
plt.xlim(1700,2015)
plt.ylabel('Sunspot Count')
plt.grid(True)



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 code above to find the largest slope in the graph. Since this valus is more than 100, following the "steepest slope of 1" rule would make the plot aspect ratio 100:1, which makes the plot too short to even read. I tried to make it as visually appealing as I could. A ratio of 20:1.5 seems to work alright. I chose a green line just to be different from default. I chose to include the grid lines because it doesn't obscure the data much and makes it helpful when determining the y-axis values near the right end of the 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:

  • 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]:
f, ax = plt.subplots(4,1, figsize = (13,5), sharey = True)

plt.sca(ax[0]) 
plt.plot(year, ssc, 'r')
plt.xlim(1700,1800)

plt.sca(ax[1])
plt.plot(year, ssc, 'r')
plt.xlim(1800,1900)

plt.sca(ax[2])
plt.plot(year, ssc, 'r')
plt.xlim(1900,2000)

plt.sca(ax[3])
plt.plot(year, ssc, 'r')
plt.xlim(2000,2100)

plt.tight_layout()



In [9]:
assert True # leave for grading