import numpy as np
import pandas as pd
precip_table_data = pd.read_csv('pandas/precip_data.csv')
from datetime import datetime
def convert_date_to_fractional_year(month, year):
'''
Given a month and a year, convert to fractional year
Inputs:
month: str
3 letter month of year
year : int
4 digit year
'''
date = datetime.strptime('{} {}'.format(month, year), '%b %Y')
fractional_year = float(date.year) + float(date.month)/12.0
return fractional_year
# filter to only monthly data
#~ means invert the condition
precip_data = precip_table_data.loc[~precip_table_data.month.isin(['Oct-Sep', 'Oct-Aug', 'Wat-Yr'])]
#Convert all dates to fractional year
date = []
for month, year in zip(precip_data.month, precip_data.year):
date.append(convert_date_to_fractional_year(month, year))
precip_data['date'] = date
#Set plotting backend and put plots in the notebook
%matplotlib inline
from matplotlib import pyplot
whr = precip_data.abbreviation == 'GAS'
pyplot.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
[<matplotlib.lines.Line2D at 0x108c1a190>]
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
pyplot.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
ax.set_xlabel('Date')
ax.set_ylabel('Precipitation')
ax.set_title('Precipication at Gasquest Ranger Station')
ax.set_ylim(-5, 30)
(-5, 30)
def format_axis(precision='%.1f',ticksize=1,axis=None,which='both'):
"""
from matplotlib.ticker import *
formats axis label precision
"""
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
if not axis:
axis = plt.gca()
if which in ('x','X','both'): axis.xaxis.set_major_formatter(FormatStrFormatter(precision))
if which in ('y','Y','both'): axis.yaxis.set_major_formatter(FormatStrFormatter(precision))
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
ax.set_xlabel('Date')
ax.set_ylabel('Precipitation')
ax.set_title('Precipication at Gasquest Ranger Station')
#---------
format_axis(which = 'x', axis = ax)
#---------
ax.set_ylim(-5, 30)
(-5, 30)
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
ax.set_xlabel('Date')
ax.set_ylabel('Precipitation')
ax.set_title('Precipication at Gasquet Ranger Station')
format_axis(which = 'x', axis = ax)
#---------
new_xticks = np.arange(2011.0, 2016.0)
ax.set_xticks(new_xticks)
#---------
ax.set_ylim(-5, 30)
(-5, 30)
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
ax.set_xlabel('Date')
ax.set_ylabel('Precipitation')
ax.set_title('Precipication at Gasquet Ranger Station')
format_axis(which = 'x', axis = ax)
new_xticks = np.arange(2011.0, 2016.0)
ax.set_xticks(new_xticks)
#---------
new_tick_labels = ['Jan 11', 'Jan 12', 'Jan 13', 'Jan 14', 'Jan 15']
ax.set_xticklabels(new_tick_labels)
#---------
ax.set_ylim(-5, 30)
(-5, 30)
#---------
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('multipage.pdf')
#---------
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
ax.set_xlabel('Date')
ax.set_ylabel('Precipitation')
ax.set_title('Precipication at Gasquet Ranger Station')
format_axis(which = 'x', axis = ax)
ax.set_ylim(-5, 30)
#---------
pp.savefig()
pyplot.close()
#---------
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
whr = precip_data.abbreviation == 'IMP'
ax.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
ax.set_xlabel('Date')
ax.set_ylabel('Precipitation')
ax.set_title('Precipication at Imperial Valley')
format_axis(which = 'x', axis = ax)
ax.set_ylim(-5, 30)
#---------
pp.savefig()
pyplot.close()
pp.close()
#---------
fig = pyplot.figure()
#---------
ax1 = fig.add_subplot(2,1,1)
#---------
whr = precip_data.abbreviation == 'GAS'
ax1.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
format_axis(which = 'x', axis = ax1)
ax1.set_ylim(-5, 30)
#---------
ax1.set_position([0, 0, 0.2, 0.7]) #[left, bottom, width, height]
#---------
#---------
ax2 = fig.add_subplot(2, 1, 2)
#---------
whr = precip_data.abbreviation == 'IMP'
ax2.plot(precip_data.date[whr], precip_data.precip[whr], 'o')
format_axis(which = 'x', axis = ax2)
ax2.set_ylim(-5, 30)
#---------
ax2.set_position([0.3, 0, 0.65, 0.7])
#---------