0% found this document useful (0 votes)
156 views

Assignment 2

This document contains instructions for Assignment 2. Students are asked to: 1) Create a line graph of record high and low temperatures from 2005-2014 with the area between shaded. 2) Overlay 2015 data points that broke previous records. 3) Remove leap day (February 29th) from the dataset. 4) Leverage principles of data visualization and make the plot nice. The dataset is from weather stations near Ann Arbor, Michigan from 2005-2015.

Uploaded by

Tiger Yan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Assignment 2

This document contains instructions for Assignment 2. Students are asked to: 1) Create a line graph of record high and low temperatures from 2005-2014 with the area between shaded. 2) Overlay 2015 data points that broke previous records. 3) Remove leap day (February 29th) from the dataset. 4) Leverage principles of data visualization and make the plot nice. The dataset is from weather stations near Ann Arbor, Michigan from 2005-2015.

Uploaded by

Tiger Yan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843
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
(https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/readme.txt) (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.figure(figsize=(8,8))

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

return mplleaflet.display()

leaflet_plot_stations(400,'fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843
b89')
Out[1]:

+
-

Leaflet | mplleaflet | Map data (c) OpenStreetMap contributors


In [8]:

df = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec
62c2c42e6cd075c2700843b89.csv')
df.sort_values('Date')
df['Data_Value'] = df['Data_Value'] * 0.1
df['Year'] = df['Date'].apply(lambda x: x[:4])
df['Date(M-D)'] = df['Date'].apply(lambda x: x[-5:])
df = df[df['Date(M-D)'] != '02-29']
df_2005_2014 = df[~(df['Year'] == '2015')]
df_2015 = df[df['Year'] == '2015']
df_2005_2014.head()

Out[8]:

ID Date Element Data_Value Year Date(M-D)

0 USW00094889 2014-11-12 TMAX 2.2 2014 11-12

1 USC00208972 2009-04-29 TMIN 5.6 2009 04-29

2 USC00200032 2008-05-26 TMAX 27.8 2008 05-26

3 USC00205563 2005-11-11 TMAX 13.9 2005 11-11

4 USC00200230 2014-02-27 TMAX -10.6 2014 02-27

In [18]:

import numpy as np
max_2004_2015 = df_2005_2014.groupby('Date(M-D)').agg({'Data_Value':np.max})
min_2004_2015 = df_2005_2014.groupby('Date(M-D)').agg({'Data_Value':np.min})
max_2015 = df_2015.groupby('Date(M-D)').agg({'Data_Value':np.max})
min_2015 = df_2015.groupby('Date(M-D)').agg({'Data_Value':np.min})

In [19]:

max_2004_2015.head()

Out[19]:

Data_Value

Date(M-D)

01-01 15.6

01-02 13.9

01-03 13.3

01-04 10.6

01-05 12.8
In [17]:

all_max = pd.merge(max_2004_2015.reset_index(), max_2015.reset_index(), left_ind


ex=True, on = 'Date(M-D)')
all_min = pd.merge(min_2004_2015.reset_index(), min_2015.reset_index(), left_ind
ex=True, on = 'Date(M-D)')
bmax = all_max[all_max['Data_Value_y'] > all_max['Data_Value_x']]
bmin = all_min[all_min['Data_Value_y'] < all_min['Data_Value_x']]
bmax.head()

Out[17]:

Date(M-D) Data_Value_x Data_Value_y

39 02-09 7.8 8.3

106 04-17 24.4 27.8

126 05-07 25.6 30.6

127 05-08 31.7 33.3

130 05-11 29.4 30.6

In [25]:

%matplotlib inline
plt.figure(figsize=(16,10))

plt.plot(max_2004_2015.values, c = 'red', label ='Record High')


plt.plot(min_2004_2015.values, c = 'green', label ='Record Low')

plt.xlabel('Day', fontsize=20)
plt.ylabel('Temperature', fontsize=20)
plt.title('Temperature Record During 2005-2014 Broken in 2015', fontsize=25)

plt.scatter(bmax.index.tolist(), bmax['Data_Value_y'].values, c = 'black', label


= "Broken High in 2015")
plt.scatter(bmin.index.tolist(), bmin['Data_Value_y'].values, c = 'green', label
= "Broken Low in 2015")
plt.legend(loc = 8, fontsize=18, frameon = False)
plt.gca().fill_between(range(len(max_2004_2015)),
np.array(max_2004_2015.values.reshape(len(min_2004_2015.v
alues),)),
np.array(min_2004_2015.values.reshape(len(min_2004_2015.v
alues),)),
facecolor='#2F99B4',
alpha=0.25)
Out[25]:

<matplotlib.collections.PolyCollection at 0x7fe04195c748>

In [ ]:

In [ ]:

You might also like