Assignment 2

Before working on this assignment please read these instructions fully. In the submission area, you will notice that you can click the link to Preview the Grading for each step of the assignment. This is the criteria that will be used for peer grading. Please familiarize yourself with the criteria before beginning the assignment.

An NOAA dataset has been stored in the file data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv. This is the dataset to use for this assignment. Note: The data for this assignment comes from a subset of The National Centers for Environmental Information (NCEI) Daily Global Historical Climatology Network (GHCN-Daily). The GHCN-Daily is comprised of daily climate records from thousands of land surface stations across the globe.

Each row in the assignment datafile corresponds to a single observation.

The following variables are provided to you:

  • id : station identification code
  • date : date in YYYY-MM-DD format (e.g. 2012-01-24 = January 24, 2012)
  • element : indicator of element type
    • TMAX : Maximum temperature (tenths of degrees C)
    • TMIN : Minimum temperature (tenths of degrees C)
  • value : data value for element (tenths of degrees C)

For this assignment, you must:

  1. Read the documentation and familiarize yourself with the dataset, then write some python code which returns a line graph of the record high and record low temperatures by day of the year over the period 2005-2014. The area between the record high and record low temperatures for each day should be shaded.
  2. Overlay a scatter of the 2015 data for any points (highs and lows) for which the ten year record (2005-2014) record high or record low was broken in 2015.
  3. Watch out for leap days (i.e. February 29th), it is reasonable to remove these points from the dataset for the purpose of this visualization.
  4. Make the visual nice! Leverage principles from the first module in this course when developing your solution. Consider issues such as legends, labels, and chart junk.

The data you have been given is near Ann Arbor, Michigan, United States, and the stations the data comes from are shown on the map below.

In [1]:
import matplotlib.pyplot as plt
import mplleaflet
import pandas as pd

def leaflet_plot_stations(binsize, hashid):

    df = pd.read_csv('data/C2A2_data/BinSize_d{}.csv'.format(binsize))

    station_locations_by_hash = df[df['hash'] == hashid]

    lons = station_locations_by_hash['LONGITUDE'].tolist()
    lats = station_locations_by_hash['LATITUDE'].tolist()


    plt.scatter(lons, lats, c='r', alpha=0.7, s=200)

    return mplleaflet.display()

In [4]:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# read data file
df = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv')

# convert to datetime format and create columns for month and day of the year
df.loc[:, "Date"] = pd.to_datetime(df["Date"])
df.loc[:, "Day"] = df["Date"]
df.loc[:, "Month"] = df["Date"].dt.month
df.loc[:, "Year"] = df["Date"].dt.year

# convert to degrees C
df.loc[:, "Data_Value"] = df["Data_Value"]/10

# exclude 29 of Februari for dataset
df_exclude = df.query("Month == 2 and Day == 29")
df = df[~df.index.isin(df_exclude.index)]

# make series for line plots 
min_temps = df.query("Year != 2015").query("Element == 'TMIN'").groupby(["Month", "Day"]).min()["Data_Value"]
max_temps = df.query("Year != 2015").query("Element == 'TMAX'").groupby(["Month", "Day"]).max()["Data_Value"]

# make series for scatter plots
min_2015 = df.query("Year == 2015").query("Element == 'TMIN'").groupby(["Month", "Day"]).min()["Data_Value"]
max_2015 = df.query("Year == 2015").query("Element == 'TMAX'").groupby(["Month", "Day"]).max()["Data_Value"]

# adjust for only record highs and record lows
min_2015 = min_2015.mask(min_2015 > min_temps)
max_2015 = max_2015.mask(max_2015 < max_temps)
In [7]:
# set the canvas
plt.figure(figsize=[15.36, 11.52])

# make plots
pos = np.arange(len(min_temps))
plt.plot(pos, max_temps.values, color="green", linewidth=2, label="record highs 2005-2014")
plt.plot(pos, min_temps.values, color="blue", linewidth=2, label="record lows 2005-2014")
plt.scatter(pos, max_2015.values, color="red", linewidth=2, label="record breaking highs 2015")
plt.scatter(pos, min_2015.values, color="black", linewidth=2, label="record breaking lows 2015")

# tidy up
                       min_temps, max_temps, 

plt.xlabel('day number')
plt.ylabel('degrees C')
plt.title("2015 record breaking temperatures against the ten year (2005-2014) record temperatures in Ann Arbor, Michigan, United States ")
In [ ]: