1 %matplotlib inline 2 3 import matplotlib.pyplot as plt 4 import matplotlib.dates as dates 5 import matplotlib.ticker as ticker 6 7 import pandas as pd 8 import numpy as np 9 10 data = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv') 11 12 # Convert all data to degrees Celsius: 13 data['Data_Value'] = data['Data_Value']*0.1 14 15 data['Date'] = pd.to_datetime(data['Date']) 16 17 data['Year'] = data['Date'].dt.year 18 19 data['Month_Day'] = data['Date'].dt.strftime('%m-%d') 20 21 # Remove February 29: 22 data = data[data['Month_Day']!='02-29'] 23 24 # Max and min data after grouping by each day: 25 max_temp = data[(data.Year >= 2005) & (data.Year < 2015) & (data['Element'] == 'TMAX')].groupby(['Month_Day'])['Data_Value'].max() 26 min_temp = data[(data.Year >= 2005) & (data.Year < 2015) & (data['Element'] == 'TMIN')].groupby(['Month_Day'])['Data_Value'].min() 27 28 # Record breaking data: 29 data = data.merge(max_temp.reset_index(drop=False).rename(columns={'Data_Value':'Max_temp'}), on='Month_Day', how='left') 30 data = data.merge(min_temp.reset_index(drop=False).rename(columns={'Data_Value':'Min_temp'}), on='Month_Day', how='left') 31 32 record_high = data[(data.Year==2015)&(data.Data_Value > data.Max_temp)] 33 record_low = data[(data.Year==2015)&(data.Data_Value < data.Min_temp)] 34 35 # X Axis spanning 2015 only: 36 date_index = np.arange('2015-01-01','2016-01-01', dtype='datetime64[D]') 37 38 plt.figure() 39 40 plt.plot(date_index,max_temp,color='lightcoral', linewidth=1) # Max in red. 41 plt.plot(date_index,min_temp,color='skyblue', linewidth=1) # Min in blue. 42 43 plt.scatter(record_high.Date.values, record_high.Data_Value.values, color='red', s=8) 44 plt.scatter(record_low.Date.values, record_low.Data_Value.values, color='blue', s=8) 45 46 # Set x and y limits. 47 ax = plt.gca() 48 ax.axis(['2015/01/01','2015/12/31',-50,50]) 49 50 # Set axis names and title: 51 plt.xlabel('Date', fontsize=10) 52 plt.ylabel('° Celsius', fontsize=10) 53 plt.title('Temperature in Ann Arbour, Michigan (2005-2015)', fontsize=12) 54 55 # Create legend and title 56 # loc=0 provides the best position for the legend 57 plt.legend(['Record high (2005-2014)','Record low (2005-2014)','Record breaking high in 2015','Record breaking low in 2015'],loc=0,frameon=False) 58 59 # Fill colour between highs and lows: 60 # alpha adjusts darkness of the shade. 61 ax.fill_between(date_index, max_temp, min_temp, facecolor='grey', alpha=0.25) 62 63 # Where you locate the major and minor ticks: 64 ax.xaxis.set_major_locator(dates.MonthLocator()) 65 ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15)) # Put the label at the minor tick so it's in the center. 66 #ax.yaxis.set_minor_locator() 67 68 # What you put at the ticks: 69 ax.xaxis.set_major_formatter(ticker.NullFormatter()) 70 ax.xaxis.set_minor_formatter(dates.DateFormatter('%b')) 71 72 # 1 refers to the bottom of the plot for xticks and the left for yticks 73 # 2 refers to the top of the plot for xticks and the right for yticks 74 for tick in ax.xaxis.get_minor_ticks(): 75 tick.tick1line.set_markersize(0) # Make small ticker disappear 76 tick.label1.set_horizontalalignment('center')