In [47]:
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
import numpy as np
from matplotlib.ticker import FixedLocator, FixedFormatter
#import seaborn as sns
from matplotlib.ticker import FixedLocator, FixedFormatter
import seaborn as sns
In [48]:
ls
In [49]:
df_ind_items = pd.read_csv('raw_data_items.csv',header=0,index_col=0,parse_dates=0)
df_ind_items.head()
Out[49]:
In [50]:
df_ind_items.index
Out[50]:
In [51]:
df_infl_items = df_ind_items.pct_change(periods=12)*100
mask_rows_infl = df_infl_items.index.year >= 2000
df_infl_items = df_infl_items[mask_rows_infl]
df_infl_items.tail()
Out[51]:
In [52]:
tt = df_infl_items.copy()
tt['month'] = tt.index.month
tt['year'] = tt.index.year
tt.head()
Out[52]:
In [53]:
tt.to_csv('infl_items.csv')
df_infl_items.rename(columns = dic)
tt = df_infl_items.copy() tt['month'] = tt.index.month tt['year'] = tt.index.year melted_df = pd.melt(tt,id_vars=['month','year']) melted_df.head()
In [54]:
df_infl_items['min'] = df_infl_items.apply(min,axis=1)
df_infl_items['max'] = df_infl_items.apply(max,axis=1)
df_infl_items['mean'] = df_infl_items.apply(np.mean,axis=1)
df_infl_items['mode'] = df_infl_items.quantile(q=0.5, axis=1)
df_infl_items['10th'] = df_infl_items.quantile(q=0.10, axis=1)
df_infl_items['90th'] = df_infl_items.quantile(q=0.90, axis=1)
df_infl_items['25th'] = df_infl_items.quantile(q=0.25, axis=1)
df_infl_items['75th'] = df_infl_items.quantile(q=0.75, axis=1)
In [55]:
df_infl_items.tail()
Out[55]:
df_infl_items['month'] = df_infl_items.index.month df_infl_items['year'] = df_infl_items.index.year
In [56]:
df_infl_items.head()
Out[56]:
In [57]:
print(df_infl_items.describe())
with plt.style.context('https://gist.githubusercontent.com/rhiever/d0a7332fe0beebfdc3d5/raw/223d70799b48131d5ce2723cd5784f39d7a3a653/tableau10.mplstyle'): for column in df_infl_items.columns[:-2]:
#if column in ['date']:
# continue
plt.figure()
plt.hist(df_infl_items[column].values)
plt.title(column)
#plt.savefig('{}.png'.format(column))
In [58]:
len(df_infl_items)
Out[58]:
In [59]:
df_infl_items.columns
Out[59]:
In [60]:
df_infl_items['month_order'] = range(len(df_infl_items))
month_order = df_infl_items['month_order']
max_infl = df_infl_items['max'].values
min_infl = df_infl_items['min'].values
mean_infl = df_infl_items['mean'].values
mode_infl = df_infl_items['mode'].values
p25th = df_infl_items['25th'].values
p75th = df_infl_items['75th'].values
p10th = df_infl_items['10th'].values
p90th = df_infl_items['90th'].values
inflEA = df_infl_items['76451'].values
In [61]:
year_begin_df = df_infl_items[df_infl_items.index.month == 1]
year_begin_df;
In [62]:
year_beginning_indeces = list(year_begin_df['month_order'].values)
year_beginning_indeces
Out[62]:
In [63]:
year_beginning_names = list(year_begin_df.index.year)
year_beginning_names
Out[63]:
In [64]:
inflEA[inflEA.argmin()]
inflEA[inflEA.argmax()]
Out[64]:
In [65]:
hist_low,ind_hist_low = min(inflEA), inflEA.argmin()
hist_high,ind_hist_high = max(inflEA), inflEA.argmax()
In [66]:
print(hist_high)
print(ind_hist_high)
In [67]:
print(min(inflEA))
print(max(inflEA))
In [68]:
blue3 = tuple(x/255 for x in [24, 116, 205]) # 1874CD
wheat2 = tuple(x/255 for x in [238, 216, 174])
wheat3 = tuple(x/255 for x in [205, 186, 150])
wheat4 = tuple(x/255 for x in [139, 126, 102])
firebrick3 = tuple(x/255 for x in [205, 38, 38])
gray30 = tuple(x/255 for x in [77, 77, 77])
In [69]:
idx = month_order
fig, ax = plt.subplots(figsize=(20, 10), subplot_kw={'axisbg': 'white'},
facecolor='white')
# plot the high-low bars
plt.vlines(idx, p10th, p90th, color=wheat3, alpha=.9,
linewidth=2.0);
#ax.vlines(idx, past_stats.lower, past_stats.upper, color=wheat3, alpha=.9,
# linewidth=1.5, zorder=-1)
# plot the confidence interval around the means
plt.vlines(idx, p25th, p75th, linewidth=2.5,
color=wheat4, zorder=-1)
# plot the present year time-series
plt.plot(idx,inflEA, color='k',linewidth=2, zorder=10);
# plot the made-up 2014 range. don't know what this was supposed to show.
ax.vlines(idx[len(idx) // 8 + 2], -4, -1, linewidth=5, color=wheat2)
ax.vlines(idx[len(idx) // 8 + 2], -3, -2, linewidth=5, color=wheat4)
#ax.errorbar(len(idx) // 8 + 3, -2.5, yerr=.5, capsize=2, capthick=1,
# color='black')
ax.text(len(idx) // 8 + 4, -2.5, "IQR", verticalalignment='center')
ax.text(len(idx) // 8 + 4, -1.2, "90 %-tile", verticalalignment='top')
ax.text(len(idx) // 8 + 4, -3.8, "10 %-tile", verticalalignment='top')
#ax.text(len(idx) // 2 - 1, 9, "2014 Temperature",
# horizontalalignment='right')
ax.plot(ind_hist_high, hist_high, 'ro',markersize=10)
ax.plot(ind_hist_low, hist_low, 'bo',markersize=10)
ax.annotate("historical low",
xy=(ind_hist_low,hist_low), xytext=(50, -45),
textcoords='offset points', #arrowprops=dict(facecolor='blue',
# arrowstyle="->",
#connectionstyle="angle3",
# width=2,
# headwidth=0,
# shrink=.02),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=0.2'),
#arrowprops=dict(arrowstyle='->', lw= 4, color= 'blue')
color='blue', horizontalalignment='left')
ax.annotate("historical high",
xy=(ind_hist_high,hist_high), xytext=(ind_hist_high + 0, 6),
textcoords='offset points',
#arrowprops=dict(facecolor='red',width=2,headwidth=0,shrink=.02),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=0.2'),
color='red', horizontalalignment='center')
##############
## formatting
#
plt.xticks(year_beginning_indeces,
year_beginning_names,
fontsize=12)
left_spine = ax.spines['left']
left_spine.set_visible(True)
left_spine.set_color(wheat4)
left_spine.set_linewidth(2)
ax.xaxis.set_ticklabels([])
ax.xaxis.grid(color=wheat3, linestyle='dotted',linewidth=2)
plt.xticks(year_beginning_indeces,
year_beginning_names,
fontsize=12)
plt.xlim(-5,200)
yticks = (range(-4, 10, 2))
ax.yaxis.set_ticks(yticks)
ylabels = [str(i) + u"%" for i in yticks]
ax.yaxis.set_ticklabels(ylabels, fontsize=14)
ax.yaxis.grid(color='white', zorder=1)
ax.set_title("Headline and disaggregated inflation, Jan 2000 - May 2016", loc="left",
fontsize=23)
ax.text(0, 8.5, "annual rate of change", fontsize=15,
fontdict=dict(weight='bold'))
ax.set_xlim(-5,200)
ax.set_ylim(-5, 9)
fig.savefig("Inflation-Items.svg")
fig.savefig("Inflation-Items.png", dpi=200)
In [ ]: