if _sp == 'no': cost = '' print(_sp) plt.plot(sp_files[_sp].sum(level='rp').index, sp_files[_sp]['event_cost'].sum(level='rp') * to_usd / 1.E6, label=_sp + cost, color=pairs_pal[_ix]) plt.plot(sp_files[_sp].sum(level='rp').index, pds_effects['dw_DELTA_' + _sp].sum(level='rp') * to_usd, label=_sp + ' benefit', ls=':', color=pairs_pal[_ix + 1]) _ix += 2 title_legend_labels(plt, 'Sri Lanka', 'Return Period [years]', 'SP cost when event occurs [M USD]', [1, 1000]) plt.xscale('log') plt.gca().get_figure().savefig(out + 'sp_costs.pdf', format='pdf') ######################################################################## # Samurdhi is a constant payout, but there is still RP-dependence # - cost of disbursement (annual & when event occurs) # - wellbeing benefit (annual & when event occurs) # - cost-benefit ratio # x-axis is targeting error # y-axis is in mUSD pds_effects = pds_effects.reset_index('rp') _wid = 0.45 rp_range = [5, 10, 25, 50, 100]
def run_poverty_duration_plot(myC): # Load file with geographical (region/province/district) as index df = pd.read_csv('../output_country/' + myC + '/poverty_duration_no.csv') df = df.reset_index().set_index(df.columns[1]) geo = df.index.name all_geo = np.array(df[~df.index.duplicated(keep='first')].index) # used in groupby df['country'] = myC # assign deciles listofdeciles = np.arange(0.10, 1.01, 0.10) df = df.reset_index().groupby( 'country', sort=True).apply(lambda x: match_percentiles( x, perc_with_spline(reshape_data(x.c), reshape_data(x.pcwgt), listofdeciles), 'decile')) # Load additional SP runs _sp = [] for f in glob.glob( '/Users/brian/Desktop/BANK/hh_resilience_model/output_country/' + myC + '/poverty_duration_*.csv'): _ = f.replace( '/Users/brian/Desktop/BANK/hh_resilience_model/output_country/' + myC + '/poverty_duration_', '').replace('.csv', '') _sp.append(_) for iSP in _sp: _ = pd.read_csv('../output_country/' + myC + '/poverty_duration_' + iSP + '.csv') df[['t_pov_inc' + iSP, 't_pov_cons' + iSP, 't_pov_bool' + iSP]] = _[['t_pov_inc', 't_pov_cons', 't_pov_bool']] ############################ # Do some plotting #plot_crit = '(t_pov_bool)&(hazard=="PF")&(rp==500)' #df.loc[df.eval(plot_crit)].plot.hexbin('dk0','t_pov_cons') #plt.gca().get_figure().savefig('../output_plots/'+myC+'/poverty_duration_hexbin_no.pdf',format='pdf') #plt.cla() #df.loc[df.eval(plot_crit)].plot.scatter('dk0','t_pov_cons') #plt.gca().get_figure().savefig('../output_plots/'+myC+'/poverty_duration_scatter_no.pdf',format='pdf') #plt.cla() ############################ df = df.reset_index().set_index(['hazard', 'rp', 'decile']) df_dec = pd.DataFrame(index=df.sum(level=['hazard', 'rp', 'decile']).index) # Populate the df_dec dataframe now, while its index is set to ['hazard','rp','decile'] # Number of individuals who face income or consumption poverty df_dec['n_new_pov_inc'] = df.loc[df.t_pov_bool == True, 'pcwgt'].sum( level=['hazard', 'rp', 'decile']) df_dec['n_new_pov_cons'] = df.loc[df.t_pov_bool == True, 'pcwgt'].sum( level=['hazard', 'rp', 'decile']) # Individuals who face income or consumption poverty as fraction of all individuals df_dec['frac_new_pov_inc'] = df_dec['n_new_pov_inc'] / df['pcwgt'].sum( level=['hazard', 'rp', 'decile']) df_dec['frac_new_pov_cons'] = df_dec['n_new_pov_cons'] / df['pcwgt'].sum( level=['hazard', 'rp', 'decile']) # Among people pushed into pov: average time in poverty (months) for iSP in _sp: df_dec['t_pov_inc_avg' + iSP] = 12. * ( df.loc[df.eval('t_pov_bool' + iSP + '==True'), ['pcwgt', 't_pov_inc' + iSP]].prod(axis=1).sum(level=['hazard', 'rp', 'decile']) / df.loc[df.eval('t_pov_bool' + iSP + '==True'), 'pcwgt'].sum(level=['hazard', 'rp', 'decile'])) df_dec['t_pov_cons_avg' + iSP] = 12. * ( df.loc[df.eval('t_pov_bool' + iSP + '==True'), ['pcwgt', 't_pov_cons' + iSP]].prod(axis=1).sum(level=['hazard', 'rp', 'decile']) / df.loc[df.eval('t_pov_bool' + iSP + '==True'), 'pcwgt'].sum(level=['hazard', 'rp', 'decile'])) for iloc in all_geo: df_dec['t_pov_inc_avg_' + iloc] = 12. * ( df.loc[df.eval('(t_pov_bool==True)&(' + geo + '==@iloc)'), ['pcwgt', 't_pov_inc']].prod(axis=1).sum( level=['hazard', 'rp', 'decile']) / df.loc[df.eval('(t_pov_bool==True)&(' + geo + '==@iloc)'), 'pcwgt'].sum(level=['hazard', 'rp', 'decile'])) df_dec['t_pov_cons_avg_' + iloc] = 12. * ( df.loc[df.eval('(t_pov_bool==True)&(' + geo + '==@iloc)'), ['pcwgt', 't_pov_cons']].prod(axis=1).sum( level=['hazard', 'rp', 'decile']) / df.loc[df.eval('(t_pov_bool==True)&(' + geo + '==@iloc)'), 'pcwgt'].sum(level=['hazard', 'rp', 'decile'])) df_dec.to_csv('../output_country/' + myC + '/poverty_by_decile.csv') ###################### # Scatter plot of hh that have to delay reconstruction upper_lim = 1E15 df['t_reco'] = (np.log(1.0 / 0.05) / df['hh_reco_rate']).clip(upper=upper_lim) means = [] xmax = 2.5E5 step = xmax / 10. for i in np.linspace(0, 10, 10): means.append(df.loc[df.eval( '(rp==1000)&(c>@i*@step)&(c<=(@i+1)*@step)&(t_reco!=@upper_lim)' ), ['pcwgt', 't_reco']].prod(axis=1).sum() / df.loc[df.eval( '(rp==1000)&(c>@i*@step)&(c<=(@i+1)*@step)&(t_reco!=@upper_lim)'), ['pcwgt']].sum()) ax = df.loc[df.eval('(c<@xmax)&(t_reco<12)')].plot.hexbin('c', 't_reco', gridsize=25, mincnt=1) plt.plot(step * np.linspace(0, 10, 10), means, zorder=100) # Do the formatting ax = title_legend_labels(ax, 'Precipitation flood in ' + myC, lab_x='Pre-disaster consumption [PhP per cap]', lab_y='Time to reconstruct [years]', leg_fs=9) # Do the saving plt.draw() plt.gca().get_figure().savefig('../output_plots/' + myC + '/t_start_reco_scatter.pdf', format='pdf') plt.cla() ###################### # Latex table of poorest quintile poverty time _cons_to_tex = df_dec.drop([ i for i in df_dec.columns if i not in ['t_pov_cons_avg_' + j for j in all_geo] ], axis=1) _inc_to_tex = df_dec.drop([ i for i in df_dec.columns if i not in ['t_pov_inc_avg_' + j for j in all_geo] ], axis=1) _cons_to_tex = _cons_to_tex.rename( columns={'t_pov_cons_avg_' + j: j for j in all_geo}).stack() _inc_to_tex = _inc_to_tex.rename( columns={'t_pov_inc_avg_' + j: j for j in all_geo}).stack() _cons_to_tex.index.names = ['hazard', 'rp', 'decile', geo] _inc_to_tex.index.names = ['hazard', 'rp', 'decile', geo] _to_tex = pd.DataFrame(index=_cons_to_tex.index) _to_tex['Income'] = _inc_to_tex.round(1) _to_tex['Consumption'] = _cons_to_tex.round(1) _to_tex = _to_tex.reset_index().set_index(geo) _to_tex = _to_tex.loc[_to_tex.eval( '(hazard=="PF")&(rp==10)&(decile==1)')].sort_values('Consumption', ascending=False) print(_to_tex.head()) _to_tex[['Income', 'Consumption']].to_latex('latex/' + myC + '_poverty_duration.tex') ###################### # Plot consumption and income poverty (separately) df_dec = df_dec.reset_index() _lab = { 't_pov_cons_avg': 'Average time to exit poverty\n(income net of reconstruction & savings) [months]', 't_pov_inc_avg': 'Average time to exit poverty (income only) [months]' } print(df_dec.columns) for ipov in ['t_pov_cons_avg', 't_pov_inc_avg']: # Do the plotting #ax = df_dec.loc[df_dec.eval('(hazard=="PF")&(rp==10)')].plot.scatter('decile',ipov+'no',color=sns_pal[1],lw=0,label='Natl. average (RP = 5 years)',zorder=99) #df_dec.loc[df_dec.eval('(hazard=="PF")&(rp==1000)')].plot.scatter('decile',ipov+'no',color=sns_pal[3],lw=0,label='Natl. average (RP = 1000 years)',zorder=98,ax=ax) ax = df_dec.loc[df_dec.eval('(hazard=="PF")&(rp==10)')].plot( 'decile', ipov + 'no', color=sns_pal[1], zorder=97, label='') df_dec.loc[df_dec.eval('(hazard=="PF")&(rp==1000)')].plot( 'decile', ipov + 'no', color=sns_pal[3], zorder=96, label='', ax=ax) icol = 4 # Which areas to plot? if myC == 'SL': focus = ['Rathnapura', 'Colombo', 'Kandy', 'Gampaha'] elif myC == 'PH': focus = ['NCR'] for iloc in focus: df_dec.loc[df_dec.eval('(hazard=="PF")&(rp==10)')].plot.scatter( 'decile', ipov + '_' + iloc, color=sns_pal[icol], lw=0, label=iloc + ' (RP = 5 years)', zorder=95, ax=ax) df_dec.loc[df_dec.eval('(hazard=="PF")&(rp==10)')].plot( 'decile', ipov + '_' + iloc, color=sns_pal[icol], zorder=94, label='', ax=ax) icol += 1 # Do the formatting ax = title_legend_labels(ax, 'Precipitation flood in ' + myC, lab_x='Decile', lab_y=_lab[ipov], lim_x=[0.5, 10.5], lim_y=[-0.1, 42], leg_fs=9) ax.xaxis.set_ticks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) ax.yaxis.set_ticks([0, 6, 12, 18, 24, 30, 36, 42]) # Do the saving ax.get_figure().savefig('../output_plots/' + myC + '/' + ipov + '_by_decile.pdf', format='pdf') plt.cla() ###################### # Plot consumption and income poverty (separately), with alternative SPs _lab = { 't_pov_cons_avg': 'Average time to exit poverty\n(income net of reconstruction & savings) [months]', 't_pov_inc_avg': 'Average time to exit poverty (income only) [months]' } icol = 0 ax = plt.gca() for ipov in ['t_pov_cons_avg', 't_pov_inc_avg']: # Do the plotting _df_5 = df_dec.loc[df_dec.eval('(hazard=="PF")&(rp==10)')].copy() _df_1000 = df_dec.loc[df_dec.eval('(hazard=="PF")&(rp==1000)')].copy() for iSP in _sp: plt.fill_between(_df_5['decile'].values, _df_5[ipov + iSP], _df_1000[ipov + iSP], alpha=0.3) _df_5.plot.scatter('decile', ipov + iSP, lw=0, label='', zorder=99, ax=ax) _df_1000.plot.scatter('decile', ipov + iSP, lw=0, label='', zorder=98, ax=ax) _df_5.plot('decile', ipov + iSP, zorder=97, linestyle=':', label=iSP + ' natl. average (RP = 5 years)', ax=ax) _df_1000.plot('decile', ipov + iSP, zorder=96, label=iSP + ' natl. average (RP = 1000 years)', ax=ax) icol += 1 # Do the formatting ax = title_legend_labels(ax, 'Precipitation flood in ' + myC, lab_x='Decile', lab_y=_lab[ipov], lim_x=[0.5, 10.5], lim_y=[-0.1], leg_fs=9) ax.xaxis.set_ticks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) ax.yaxis.set_ticks([0, 3, 6, 9, 12, 15, 18]) # Do the saving ax.get_figure().savefig('../output_plots/' + myC + '/' + ipov + '_with_sps_by_decile.pdf', format='pdf') plt.cla() plt.close('all') return True
def poverty_gap_plot( myC, _myiah, economy='region_est2', spvalue=27272.73, annual_pov_line=81.7 * 365, annual_pov_line_extreme=41.6 * 365, output_dir='/Users/jaycocks/Projects/wb_resilience/output_plots/HT/transfer_poverty_' ): #df = pd.read_csv('/Users/jaycocks/Projects/wb_resilience/intermediate/HT/cat_info.csv') # _myiah = iah.loc[(iah['hazard']=='EQ')&(iah['affected_cat']=='a')&(iah['helped_cat']=='helped'),] #_myiah = df #annual_pov_line = 81.7 * 365 #annual_pov_line_extreme = 41.6 * 365 _myiah['pov_line'] = annual_pov_line _myiah['sub_line'] = annual_pov_line_extreme #_myiah['pov_line'] = annual_pov_line_extreme #_myiah['sub_line'] = annual_pov_line_extreme * 0.7 _myiah['dc0'] = _myiah['pcinc'] #initial _myiah['dc_pre_reco'] = _myiah['pcinc'] + (spvalue / _myiah['hhsize'] ) #final _myiah = _myiah.reset_index().set_index([economy, 'hhid']) # use c_initial (dc0) & i_pre_reco (dc_pre_reco) district_df = _myiah.groupby(economy)['pcwgt'].sum().to_frame( name='population') district_df['pop_in_poverty_initial'] = _myiah.loc[ _myiah.ispoor == 1., ].groupby(economy)['pcwgt'].sum() district_df['pop_in_poverty_final'] = _myiah.loc[ _myiah.dc_pre_reco < annual_pov_line, ].groupby( economy)['pcwgt'].sum() district_df['pop_in_extpoverty_initial'] = _myiah.loc[ _myiah.ispoor_extreme == 1., ].groupby(economy)['pcwgt'].sum() district_df['pop_in_extpoverty_final'] = _myiah.loc[ _myiah.dc_pre_reco < annual_pov_line_extreme, ].groupby( economy)['pcwgt'].sum() _myiah['i_initialn'] = _myiah[['pcwgt', 'dc0']].product(axis=1) district_df['i_initial'] = _myiah.loc[_myiah.ispoor == 1., ].groupby( economy)['i_initialn'].sum() / district_df['pop_in_poverty_initial'] # district_df['i_final'] = _myiah.loc[_myiah.i_pre_reco<_myiah.pov_line].eval('pcwgt_no*i_pre_reco').sum(level='district')/district_df['pop_in_poverty_final'] _myiah['i_finaln'] = _myiah[['pcwgt', 'dc_pre_reco']].product(axis=1) district_df['i_final'] = _myiah.loc[_myiah.ispoor == 1., ].groupby( economy)['i_finaln'].sum() / district_df['pop_in_poverty_initial'] ## This is the poverty gap - the average income by region as a percent of the poverty line # 70 percent is 70 not 0.70 (see the 1E2 multiplier) district_df['gap_initial'] = 1E2 * _myiah.loc[_myiah.ispoor == 1.].eval( 'pcwgt*(dc0/pov_line)').sum( level=economy) / district_df['pop_in_poverty_initial'] ##district_df['gap_final'] = _myiah.loc[_myiah.i_pre_reco<_myiah.pov_line].eval('pcwgt_no*(i_pre_reco/pov_line)').sum(level=economy)/district_df['pop_in_poverty_final'] district_df['gap_final'] = 1E2 * _myiah.loc[_myiah.ispoor == 1.].eval( 'pcwgt*(dc_pre_reco/pov_line)').sum( level=economy) / district_df['pop_in_poverty_initial'] ## This is the poverty shortfall - the average income by region as a percent of the poverty line district_df['short_initial'] = _myiah.loc[_myiah.ispoor == 1.].eval( '-1E2*pcwgt*(1.-dc0/pov_line)').sum( level=economy) / district_df['pop_in_poverty_initial'] #district_df['gap_final'] = _myiah.loc[_myiah.i_pre_reco<_myiah.pov_line].eval('pcwgt_no*(i_pre_reco/pov_line)').sum(level='district')/district_df['pop_in_poverty_final'] district_df['short_final'] = _myiah.loc[_myiah.ispoor == 1.].eval( '-1E2*pcwgt*(1.-dc_pre_reco/pov_line)').sum( level=economy) / district_df['pop_in_poverty_initial'] # district_df = district_df.sort_values('gap_initial', ascending=False) # for _fom in ['gap', 'short']: plt.close('all') # plt.scatter(district_df.index, district_df[_fom + '_initial'], alpha=0.6, color=sns.color_palette('tab20b', n_colors=20)[15], s=15) plt.scatter(district_df.index, district_df[_fom + '_final'], alpha=0.6, color=sns.color_palette('tab20b', n_colors=20)[12], s=15) # _xlim = plt.gca().get_xlim() # if _fom == 'gap': _fsub = float( _myiah.eval('1E2*pcwgt*sub_line').sum() / _myiah.eval('pcwgt*pov_line').sum()) if _fom == 'short': _fsub = float( _myiah.eval('-1E2*pcwgt*(pov_line-sub_line)').sum() / _myiah.eval('pcwgt*pov_line').sum()) # Subsistence line - the extreme poverty line plt.plot([_xlim[0], len(district_df.index) - 0.5], [_fsub, _fsub], color=greys_pal[4], lw=1.0, alpha=0.95) if _fom == 'short': plt.plot([_xlim[0], len(district_df.index) - 0.5], [0, 0], color=greys_pal[4], lw=1.0, alpha=0.95) #plt.plot([_xlim[0], len(district_df.index) - 0.5], [_fsub, _fsub], lw=1.0, alpha=0.95) #if _fom == 'short': plt.plot([_xlim[0], len(district_df.index) - 0.5], [0, 0], lw=1.0,alpha=0.95) plt.xlim(_xlim) # plt.annotate('Poverty gap\npre-transfer', xy=(len(district_df.index) - 1, district_df[_fom + '_initial'][-1]), xytext=(len(district_df.index), district_df[_fom + '_initial'][-1]), arrowprops=dict( arrowstyle="-", facecolor=greys_pal[5], connectionstyle="angle,angleA=0,angleB=-90,rad=5"), fontsize=6.5, color=greys_pal[7], va='bottom', style='italic') plt.annotate( 'Poverty gap\npost-transfer', xy=(len(district_df.index) - 1, district_df[_fom + '_final'][-1]), xytext=(len(district_df.index), district_df[_fom + '_final'][-1]), arrowprops=dict(arrowstyle="-", facecolor=greys_pal[5], connectionstyle="angle,angleA=0,angleB=-90,rad=5"), fontsize=6.5, color=greys_pal[7], va='top', style='italic') plt.annotate('Extreme\npoverty\n', xy=(len(district_df.index) - 1, _fsub), xytext=(len(district_df.index), _fsub), arrowprops=dict( arrowstyle="-", facecolor=greys_pal[5], connectionstyle="angle,angleA=0,angleB=-90,rad=5"), fontsize=6.5, color=greys_pal[7], va='bottom', style='italic') if _fom == 'short': plt.annotate( 'Poverty\nline', xy=(len(district_df.index) - 1, 0), xytext=(len(district_df.index), 0), arrowprops=dict( arrowstyle="-", facecolor=greys_pal[5], connectionstyle="angle,angleA=0,angleB=-90,rad=5"), fontsize=6.5, color=greys_pal[7], va='bottom', style='italic') for _n, _ in enumerate(district_df.index): #if _fom == 'gap': plt.plot([_n,_n],[0,district_df.iloc[_n][_fom+'_initial']],color=greys_pal[4],alpha=0.5,ls=':',lw=0.5) #if _fom == 'short': plt.plot([_n,_n],[0,district_df.iloc[_n][_fom+'_final']],color=greys_pal[4],alpha=0.5,ls=':',lw=0.5) if _fom == 'gap': plt.plot([_n, _n], [0, district_df.iloc[_n][_fom + '_initial']], alpha=0.5, ls=':', lw=0.5) if _fom == 'short': plt.plot([_n, _n], [0, district_df.iloc[_n][_fom + '_final']], alpha=0.5, ls=':', lw=0.5) # Do the formatting if _fom == 'gap': _ylabel = 'Average income, as % of poverty line,\nfor population in poverty before Transfer)' plt.ylim(30, 90) ax = title_legend_labels(plt.gca(), '', lab_x='', lab_y=_ylabel, leg_fs=9, do_leg=False) ax.set_xticklabels(labels=district_df.index, rotation=45, ha='right', fontsize=8) sns.despine() if _fom == 'short': _ylabel = 'Average income shortfall, as % poverty line,\nfor population in poverty before Transfer)' ax = title_legend_labels(plt.gca(), '', lab_x='', lab_y=_ylabel, leg_fs=9, do_leg=False) ax.set_xticklabels(labels=district_df.index, rotation=45, ha='right', fontsize=8) ax.xaxis.set_ticks_position('top') #ax.invert_yaxis() #ax.xaxis.tick_top() plt.ylim(-60, 2) ax.tick_params(labelbottom='off', labeltop='on') sns.despine(bottom=True) plt.grid(False) # Do the saving plt.draw() plt.gca().get_figure().savefig(output_dir + _fom + str(spvalue) + '.pdf', format='pdf', bbox_inches='tight') plt.cla() return district_df
def poverty_gap_plot(myC,_myiah,event_level,myHaz,drop_spots=None,_mapres=3000): # use c_initial & i_pre_reco district_df = _myiah['pcwgt_no'].sum(level='district').to_frame(name='population') district_df['pop_in_poverty_initial'] = _myiah.loc[_myiah.ispoor==1.,'pcwgt_no'].sum(level='district') district_df['pop_in_poverty_final'] = _myiah.loc[_myiah.i_pre_reco<_myiah.pov_line,'pcwgt_no'].sum(level='district') # district_df['i_initial'] = _myiah.loc[_myiah.ispoor==1.].eval('pcwgt_no*c_initial').sum(level='district')/district_df['pop_in_poverty_initial'] #district_df['i_final'] = _myiah.loc[_myiah.i_pre_reco<_myiah.pov_line].eval('pcwgt_no*i_pre_reco').sum(level='district')/district_df['pop_in_poverty_final'] district_df['i_final'] = _myiah.loc[_myiah.ispoor==1.].eval('pcwgt_no*i_pre_reco').sum(level='district')/district_df['pop_in_poverty_initial'] # district_df['gap_initial'] = 1E2*_myiah.loc[_myiah.ispoor==1.].eval('pcwgt_no*(c_initial/pov_line)').sum(level='district')/district_df['pop_in_poverty_initial'] #district_df['gap_final'] = _myiah.loc[_myiah.i_pre_reco<_myiah.pov_line].eval('pcwgt_no*(i_pre_reco/pov_line)').sum(level='district')/district_df['pop_in_poverty_final'] district_df['gap_final'] = 1E2*_myiah.loc[_myiah.ispoor==1.].eval('pcwgt_no*(i_pre_reco/pov_line)').sum(level='district')/district_df['pop_in_poverty_initial'] # district_df['short_initial'] = _myiah.loc[_myiah.ispoor==1.].eval('-1E2*pcwgt_no*(1.-c_initial/pov_line)').sum(level='district')/district_df['pop_in_poverty_initial'] #district_df['gap_final'] = _myiah.loc[_myiah.i_pre_reco<_myiah.pov_line].eval('pcwgt_no*(i_pre_reco/pov_line)').sum(level='district')/district_df['pop_in_poverty_final'] district_df['short_final'] = _myiah.loc[_myiah.ispoor==1.].eval('-1E2*pcwgt_no*(1.-i_pre_reco/pov_line)').sum(level='district')/district_df['pop_in_poverty_initial'] # district_df = district_df.sort_values('gap_initial',ascending=False) # for _fom in ['gap','short']: plt.close('all') # plt.scatter(district_df.index,district_df[_fom+'_initial'],alpha=0.6,color=sns.color_palette('tab20b', n_colors=20)[15],s=15) plt.scatter(district_df.index,district_df[_fom+'_final'],alpha=0.6,color=sns.color_palette('tab20b', n_colors=20)[12],s=15) # _xlim = plt.gca().get_xlim() # if _fom == 'gap': _fsub = float(_myiah.eval('1E2*pcwgt_no*sub_line').sum()/_myiah.eval('pcwgt_no*pov_line').sum()) if _fom == 'short': _fsub = float(_myiah.eval('-1E2*pcwgt_no*(pov_line-sub_line)').sum()/_myiah.eval('pcwgt_no*pov_line').sum()) # Subsistence line plt.plot([_xlim[0],len(district_df.index)-0.5],[_fsub,_fsub],color=greys_pal[4],lw=1.0,alpha=0.95) if _fom == 'short': plt.plot([_xlim[0],len(district_df.index)-0.5],[0,0],color=greys_pal[4],lw=1.0,alpha=0.95) plt.xlim(_xlim) # plt.annotate('Poverty gap\npre-Idai',xy=(len(district_df.index)-1,district_df[_fom+'_initial'][-1]),xytext=(len(district_df.index),district_df[_fom+'_initial'][-1]), arrowprops=dict(arrowstyle="-",facecolor=greys_pal[5],connectionstyle="angle,angleA=0,angleB=-90,rad=5"),fontsize=6.5,color=greys_pal[7],va='bottom',style='italic') plt.annotate('Poverty gap\npost-Idai',xy=(len(district_df.index)-1,district_df[_fom+'_final'][-1]),xytext=(len(district_df.index),district_df[_fom+'_final'][-1]), arrowprops=dict(arrowstyle="-",facecolor=greys_pal[5],connectionstyle="angle,angleA=0,angleB=-90,rad=5"),fontsize=6.5,color=greys_pal[7],va='top',style='italic') plt.annotate('Extreme\npoverty\n',xy=(len(district_df.index)-1,_fsub),xytext=(len(district_df.index),_fsub), arrowprops=dict(arrowstyle="-",facecolor=greys_pal[5],connectionstyle="angle,angleA=0,angleB=-90,rad=5"),fontsize=6.5,color=greys_pal[7],va='bottom',style='italic') if _fom == 'short': plt.annotate('Poverty\nline',xy=(len(district_df.index)-1,0),xytext=(len(district_df.index),0), arrowprops=dict(arrowstyle="-",facecolor=greys_pal[5],connectionstyle="angle,angleA=0,angleB=-90,rad=5"), fontsize=6.5,color=greys_pal[7],va='bottom',style='italic') # for _n,_ in enumerate(district_df.index): if _fom == 'gap': plt.plot([_n,_n],[0,district_df.iloc[_n][_fom+'_initial']],color=greys_pal[4],alpha=0.5,ls=':',lw=0.5) if _fom == 'short': plt.plot([_n,_n],[0,district_df.iloc[_n][_fom+'_final']],color=greys_pal[4],alpha=0.5,ls=':',lw=0.5) # # Do the formatting if _fom == 'gap': _ylabel = 'Poverty gap (Average income, as % of poverty line,\nfor households in poverty before Idai)' plt.ylim(45,85) ax = title_legend_labels(plt.gca(),'',lab_x='',lab_y=_ylabel,leg_fs=9,do_leg=False) ax.set_xticklabels(labels=district_df.index,rotation=45,ha='right',fontsize=8) sns.despine() if _fom == 'short': _ylabel = 'Poverty gap (Average income shortfall, as % poverty line,\nfor households in poverty before Idai)' ax = title_legend_labels(plt.gca(),'',lab_x='',lab_y=_ylabel,leg_fs=9,do_leg=False) ax.set_xticklabels(labels=district_df.index,rotation=45,ha='right',fontsize=8) ax.xaxis.set_ticks_position('top') #ax.invert_yaxis() #ax.xaxis.tick_top() plt.ylim(-50,2) ax.tick_params(labelbottom='off',labeltop='on') sns.despine(bottom=True) plt.grid(False) # Do the saving plt.draw() plt.gca().get_figure().savefig('../output_plots/'+myC+'/idai_poverty_'+_fom+'.pdf',format='pdf',bbox_inches='tight') plt.cla()
def draw_drought_income_plot(df=None, _='initial', _rho=0.70): big_pal = sns.color_palette('tab20b', n_colors=20) if df is None: df = pd.read_csv('../output_country/MW/out_ag.csv').set_index( event_level + ['hhid']) df['frac_ag'] = df.eval('(di_ag/v_ag)/c') df['i_nonag'] = 1E-3 * df.eval('c*(1-frac_ag)') df['i_ag'] = 1E-3 * df.eval('c*(frac_ag)') df = (df.loc[(df.frac_ag >= 0.70) & (df.frac_ag < 0.99) & (df.v_ag <= 0.40)].sample(1)).squeeze() print(df) ## Non-agricultural income #plt.plot([0,2],[df['c_nonag'],df['c_nonag']]) ######################### # Agricultural income spenddown _x = [] _y = [] _x_lean = [] _y_lean = [] _xDR = [] _yDR = [] _xDR_lean = [] _yDR_lean = [] t_series, _dt = np.linspace(0, 2, 4E2, retstep=True) c_ag_init = _rho * df['i_ag'] / (1 - np.exp(-_rho)) for _t in t_series: _sf = 1 if _t < 1 else (1 - df['v_ag']) _tcyc = _t if _t < 1 else _t - 1 # _cag_t = c_ag_init * _sf * np.exp(-_rho * _tcyc) # if _t <= 1: _x.append(_t) _y.append(_cag_t) if _cag_t <= df['i_ag'] * _sf: _x_lean.append(_t) _y_lean.append(_cag_t) else: _xDR.append(_t) _yDR.append(_cag_t) if _cag_t <= df['i_ag'] * _sf: _xDR_lean.append(_t) _yDR_lean.append(_cag_t) # Plot c_ag(t) plt.plot(_x, _y, color=greys_pal[7], alpha=0.9, zorder=100) plt.plot(_xDR, _yDR, color=greys_pal[7], alpha=0.9, zorder=100) # plt.plot([_ - 1 for _ in _x], _y, color=greys_pal[7], alpha=0.9, zorder=100, clip_on=True) plt.plot([_ + 2 for _ in _x], _y, color=greys_pal[7], alpha=0.9, zorder=100, clip_on=True) # plt.plot([0, 0], [_y[-1], _y[0]], color=greys_pal[7], alpha=0.9, zorder=100) # connect year = -1 to year = 0 plt.plot([1, 1], [_y[-1], _yDR[0]], color=greys_pal[8], alpha=0.9, zorder=100) # connect year = 0 to year = 1 plt.plot([2, 2], [_yDR[-1], _y[0]], color=greys_pal[7], alpha=0.9, zorder=100) # connect year = 1 to year = 2 # and do annotation #_str = 'Household consumption\nof agricultural income\n$c_{ag}(t)\,=\,C^{\prime}_{ag}\cdot e^{-\gamma_{ag}t}$\n(cf. Eqs. 3$\,$&$\,$7)' _str = 'Household consumption\nof agricultural income $c_{ag}(t)$\n(cf.$\,$Eqs.$\,$2-7)' plt.annotate(_str, xy=(_x[5], _y[1]), xytext=(_x[13], _y[0]), arrowprops=dict( arrowstyle="-", facecolor=greys_pal[7], connectionstyle="angle,angleA=0,angleB=-135,rad=0"), fontsize=7.5, color=greys_pal[7], va='bottom', ha='left', linespacing=1.4) ######################### # Agricultural income spike for yr_no in [0, 1, 2]: # annual income squeezed into 1 month len_harvest = 4 # months _sf = 1 if yr_no == 1: _sf = (1 - df['v_ag']) plt.plot([yr_no, yr_no], [0, (12 / len_harvest) * df['i_ag'] * _sf], color=big_pal[10], alpha=0.5) plt.plot([yr_no, yr_no + len_harvest / 12], [(12 / len_harvest) * df['i_ag'] * _sf, (12 / len_harvest) * df['i_ag'] * _sf], color=big_pal[10], alpha=0.5) plt.fill_between([yr_no, yr_no + len_harvest / 12], [(12 / len_harvest) * df['i_ag'] * _sf, (12 / len_harvest) * df['i_ag'] * _sf], color=big_pal[11], alpha=0.5) plt.plot([yr_no + len_harvest / 12, yr_no + len_harvest / 12], [0, (12 / len_harvest) * df['i_ag'] * _sf], color=big_pal[10], alpha=0.5) # # Mark lean season if yr_no != 2: plt.fill_between( (_x_lean if yr_no == 0 else _xDR_lean)[:-1], y1=(_y_lean if yr_no == 0 else _yDR_lean)[:-1], #y2=[0 for _tcyc in _y_lean[:-1]], color=big_pal[13], alpha=0.1) # and do annotation if yr_no == 0: plt.annotate('Lean season', xy=((_xDR_lean if yr_no == 1 else _x_lean)[-4], df['i_ag'] * (0.03)), fontsize=7, color=big_pal[13], va='bottom', ha='right') # # Plot annualized income as dotted line plt.plot([yr_no, yr_no + 1], [df['i_ag'] * _sf, df['i_ag'] * _sf], color=greys_pal[4], alpha=0.4, ls='--', lw=0.75) # and do annotation _labstr = ('Income from main\nharvest, normal year' if yr_no == 0 else 'Income from main\nharvest, drought year') plt.annotate( _labstr, xy=(yr_no + len_harvest / 12 * 0.20, (12 / len_harvest) * df['i_ag'] * _sf), xytext=(yr_no + len_harvest / 12 * (0.20 * 1.75), (12 / len_harvest) * df['i_ag'] * (_sf + 0.06)), arrowprops=dict( arrowstyle="-", facecolor=greys_pal[7], connectionstyle="angle,angleA=0,angleB=-90,rad=5"), fontsize=7.5, color=greys_pal[7], va='center', ha='left') # ticks plt.xticks([0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.50, 1.75, 2]) plt.gca().set_xticklabels(labels=[ 'April', 'July', 'October', 'January', 'April', 'July', 'October', 'January', 'April' ], ha='right', fontsize=8, rotation=45) plt.tick_params(axis='x', which='major', color=greys_pal[4]) plt.tick_params(axis='y', which='major', color=greys_pal[4], labelsize=8) # title_legend_labels(plt.gca(), '', lab_x='', lab_y='Kwacha per capita (,000)', lim_x=None, lim_y=None, leg_fs=9, do_leg=False) # plt.grid(False) plt.xlim(-0.05, 2.05) plt.ylim(0) sns.despine() plt.gcf().savefig('../output_plots/MW/drought/schematic.pdf', format='pdf', bbox_inches='tight') plt.close('all')
def plot(self, c1=None, c2=None, c3=None, c4=None): x_shift = 2. # Plot the fraction of winners plt.plot([0, 100], [0, 0], linewidth=1.5, color=greys[5], zorder=1) for _c in [c1, c2, c3, c4]: if _c is not None: _ls = '-' if _c.incl_targeting_corr is not None: _ls = '--' plt.plot(_c.scale_out, _c.impact_winners, color=_c.pdict['scaleout_' + _c.sp + '_100col'], linestyle=_ls) ##### # Find/plot/annotate max #plt.scatter(_c.scale_out[_c.impact_winners.index(max(_c.impact_winners))],max(_c.impact_winners),color=_c.pdict['scaleout_'+_c.sp+'_100col']) _opty = max(_c.impact_winners) _optx = _c.scale_out[_c.impact_winners.index(_opty)] plt.plot([_optx, _optx], [_opty, _opty + 0.5], color=greys[4], linewidth=1.2) _init_enroll = 100. * (_c.fracs[1] + _c.fracs[2]) / 2. plt.annotate('Increase by\n' + str(round(_optx - _init_enroll, 1)) + '% in all quintiles', xy=(_optx, _opty + 0.75), size=5, va='bottom', ha='center') ##### if _c.incl_targeting_corr is None: _anno = _c.sp.replace('ct', 'Cash\ntransfer').replace( 'trsgob', 'Non-cash\ntransfer') plt.annotate(_anno, xy=(_c.scale_out[0] - x_shift, _c.impact_winners[0]), weight='bold', color=greys[6], ha='right', va='center', size=5.5) else: _anno = 'Reduce inclusion\nerror by 10%' plt.annotate(_anno, xy=(_c.scale_out[0] + x_shift, _c.impact_winners[0]), weight='bold', color=greys[5], ha='left', va='center', size=5) plt.xlim(0, 100) lgd = title_legend_labels( plt.gca(), iso_to_name[self.pais], lab_x='Scale out (% of each quintile enrolled)', lab_y='Percent of poorest 40% that benefits', do_leg=False) sns.despine(bottom=True) plt.gcf().savefig('output/sp/' + self.pais + '_optimal_sp_winners.pdf', format='pdf', bbox_inches='tight') plt.cla() # Plot relative impact (summed at quintile-level) plt.plot([0, 100], [0, 0], linewidth=1.5, color=greys[5], zorder=1) for _c in [c1, c2, c3, c4]: if _c is not None: _ls = '-' if _c.incl_targeting_corr is not None: _ls = '--' plt.plot(_c.scale_out, _c.impact_rel, color=_c.pdict['scaleout_' + _c.sp + '_100col'], linestyle=_ls) if _c.incl_targeting_corr is None: _anno = _c.sp.replace('ct', 'Cash\ntransfer').replace( 'trsgob', 'Non-cash\ntransfer') plt.annotate(_anno, xy=(_c.scale_out[0] - x_shift, _c.impact_rel[0]), weight='bold', color=greys[6], ha='right', va='center', size=5.5) else: _anno = 'Reduce inclusion\nerror by ' + str( int(100 * _c.incl_targeting_corr)) + '%' plt.annotate(_anno, xy=(_c.scale_out[0] + x_shift, _c.impact_rel[0]), weight='bold', color=greys[5], ha='left', va='center', size=5) plt.xlim(0, 100) lgd = title_legend_labels( plt.gca(), iso_to_name[self.pais], lab_x='Scale out (% of bottom 40% enrolled)', lab_y= 'Net impact on bottom 40%\n(relative to quintile expenditures)', do_leg=False) sns.despine(bottom=True) plt.gcf().savefig('output/sp/' + self.pais + '_optimal_sp_rel_impact.pdf', format='pdf', bbox_inches='tight') plt.cla() # Plot relative impact (calculated at household level) plt.plot([0, 100], [0, 0], linewidth=1.5, color=greys[5], zorder=1) for _c in [c1, c2, c3, c4]: if _c is not None: _ls = '-' if _c.incl_targeting_corr is not None: _ls = '--' plt.plot(_c.scale_out, _c.impact_rel_by_hh, color=_c.pdict['scaleout_' + _c.sp + '_100col'], linestyle=_ls) if _c.incl_targeting_corr is None: _anno = _c.sp.replace('ct', 'Cash\ntransfer').replace( 'trsgob', 'Non-cash\ntransfer') plt.annotate(_anno, xy=(_c.scale_out[0] - x_shift, _c.impact_rel_by_hh[0]), weight='bold', color=greys[6], ha='right', va='center', size=5.5) else: _anno = 'Reduce inclusion\nerror by ' + str( int(100 * _c.incl_targeting_corr)) + '%' plt.annotate(_anno, xy=(_c.scale_out[0] + x_shift, _c.impact_rel_by_hh[0]), weight='bold', color=greys[4], ha='left', va='center', size=5) plt.xlim(0, 100) lgd = title_legend_labels( plt.gca(), iso_to_name[self.pais], lab_x='Scale out (% of bottom 40% enrolled)', lab_y= 'Net impact on bottom 40%\n(relative to household expenditures)', do_leg=False) sns.despine(bottom=True) plt.gcf().savefig('output/sp/' + self.pais + '_optimal_sp_rel_impact_by_hh.pdf', format='pdf', bbox_inches='tight') plt.cla() # Plot average impact, in $ plt.plot([0, 100], [0, 0], linewidth=1.5, color=greys[5], zorder=1) for _c in [c1, c2, c3, c4]: if _c is not None: _ls = '-' if _c.incl_targeting_corr is not None: _ls = '--' plt.plot(_c.scale_out, _c.impact_abs, color=_c.pdict['scaleout_' + _c.sp + '_100col'], linestyle=_ls) if _c.incl_targeting_corr is None: _anno = _c.sp.replace('ct', 'Cash\ntransfer').replace( 'trsgob', 'Non-cash\ntransfer') plt.annotate(_anno, xy=(_c.scale_out[0] - x_shift, _c.impact_abs[0]), weight='bold', color=greys[6], ha='right', va='center', size=5.5) else: _anno = 'Reduce inclusion\nerror by ' + str( int(100 * _c.incl_targeting_corr)) + '%' plt.annotate(_anno, xy=(_c.scale_out[0] + x_shift, _c.impact_abs[0]), weight='bold', color=greys[4], ha='left', va='center', size=5) plt.xlim(0, 100) lgd = title_legend_labels(plt.gca(), iso_to_name[self.pais], lab_x='Scale out (% of bottom 40% enrolled)', lab_y='Impact on bottom 40% [INT$]', do_leg=False) sns.despine(bottom=True) plt.gcf().savefig('output/sp/' + self.pais + '_optimal_sp_abs_impact.pdf', format='pdf', bbox_inches='tight') plt.cla() plt.close('all')