def make_plot(climate_data='agmerra'): sns.set() sns.set_context("notebook") sns.set_style('ticks') colors = define_colors() b_prec, b_tmax = fig_data(climate_data=climate_data) #b_prec = pd.read_csv('../data/result/agmip_prec_bin.csv', index_col=0) # Sample plot use step, show response of individual model but no names palette = sns.color_palette() x_txt = [str(i) for i in np.arange(-2.5, 3.6, 0.5)] x_txt.insert(0, '') x_txt.append('') fig, ax = plt.subplots(1) ax.plot([1, 15], [0, 0], color='k', lw=0.5, linestyle='--') temp = append_value(b_prec['obs']) l1, = ax.step(range(1, 16), temp, where='post', color='#1A1834', lw=3, alpha=0.9) temp = append_value(b_prec.iloc[:, 1::].median(axis=1)) l2, = ax.step(range(1, 16), temp, where='post', color=palette[1], lw=3) # for i in range(1,13): # b_prec.iloc[:,i] # l3, = ax.step(range(1,16)+np.random.rand(15)/10, append_value(b_prec.iloc[:,i]), where='post', color=palette[1], # lw=0.75, alpha=0.5,linestyle='-') # ax.set_ylim(-0.5,0.5) ax.set_ylim(-75, 75) ax.set_xlim(1, 15) ax.set(xticks=np.arange(1, 15, 1), xticklabels=(x_txt)) ax.set_ylabel('Yield change (%)', fontsize=12) ax.set_xlabel('Precipitation anomaly ($\sigma$)', labelpad=15, fontsize=12) ax.text(0.00 + 0.04, -0.1, 'Extremely dry', transform=ax.transAxes, fontsize=10, color=colors[0]) ax.text(0.25 + 0.04, -0.1, 'Moderate dry', transform=ax.transAxes, fontsize=10, color=colors[2]) ax.text(0.5 + 0.04, -0.1, 'Moderate wet', transform=ax.transAxes, fontsize=10, color=colors[6]) ax.text(0.75 + 0.04, -0.1, 'Extremely wet', transform=ax.transAxes, fontsize=10, color=colors[-1]) [ t.set_color(colors[0]) for i, t in enumerate(ax.xaxis.get_ticklabels()) if i < 3 ] [ t.set_color(colors[-1]) for i, t in enumerate(ax.xaxis.get_ticklabels()) if i >= 11 ] # max and min range p1 = ax.fill_between(range(1, 16), append_value(b_prec.iloc[:, 1::].min(axis=1)), append_value(b_prec.iloc[:, 1::].max(axis=1)), alpha=0.3, color=palette[1], step='post') # interquantile range p2 = ax.fill_between(range(1, 16), append_value(b_prec.iloc[:, 1::].quantile(0.25, axis=1)), append_value(b_prec.iloc[:, 1::].quantile(0.75, axis=1)), alpha=0.5, color=palette[1], step='post') ax.legend([l1, l2, p2, p1], ['Obs', 'AgMIP median', 'AgMIP interquantile', 'AgMIP range'], loc='upper left') plt.subplots_adjust(bottom=0.2) plt.savefig('../figure/figure4_%s.pdf' % climate_data) print('Figure saved for %s' % climate_data)
import matplotlib.pyplot as plt import numpy as np import pandas as pd import statsmodels.api as sm import seaborn as sns from load_prism_data import load_gs_climate from plot_figure1 import define_colors from plot_figure3 import plot_scatter_sns, figure_data colors = define_colors() level = 'State' rain_state_w, drought_state_w = figure_data(level=level) # Load tile drainage data tile = pd.read_csv('../../data/US_tile/drainage_shapefile.csv', usecols=['STATE_NAME', 'FIPS', 'tileDra'], dtype={'FIPS': str}) tile_st = tile.groupby('STATE_NAME').mean().sort_values( by='tileDra', ascending=False).reset_index().copy() tile_st.rename(columns={'STATE_NAME': 'State'}, inplace=True) tile_st['State'] = tile_st['State'].str.upper() tile_st.loc[:, 'tileDra'] = tile_st.loc[:, 'tileDra'] * 100 data_m = rain_state_w.merge(tile_st, on='State') #print(data_m) #color_var={'Prec_mean_weighted':colors[-1], 'Tmax_mean_weighted':colors[0], # 'Area':'#525252', soilvar+'_weighted':'#995D12'}
def make_plot(): # Load and combine data bin_yield = pd.read_csv( '/home/yanli/Project/corn_extreme/data/result/bin_yield.csv', dtype={'FIPS': str}) hrain = pd.read_csv( '/home/yanli/Project/corn_extreme/data/result/heavy_rain_percent.csv', dtype={'FIPS': str}) bin_yield_hrain = bin_yield.merge(hrain, on=['FIPS', 'Year']) colors = define_colors() # Begin plot fig, ax = plt.subplots(1, figsize=(8, 6)) (bin_yield_hrain.groupby('Prec_sigma_bin').mean(). loc[:, slice('HPrec_percent_0', 'HPrec_percent_8')] * 100).plot( legend=False, ax=ax, marker='.') legend_txt = [ '<0$\sigma$', '(0$\sigma$,0.5$\sigma$)', '(0.5$\sigma$,1$\sigma$)', '(1$\sigma$,1.5$\sigma$)', '(1.5$\sigma$,2$\sigma$)', '(2$\sigma$,2.5$\sigma$)', '(2.5$\sigma$,3$\sigma$)', '(3$\sigma$,3.5$\sigma$)', '>3.5$\sigma$ (Heavyrain)' ] ax.legend(ax.lines, (legend_txt), loc='upper center', frameon=False, title='Daily rainfall intensity', ncol=2) ax.set_xlabel('Precipitation anomaly of growning season', fontsize=12) ax.set_ylabel('Percentage (%)', fontsize=12) ax.set_title( 'Contribution of daily rainfall of different intensities to \n growing season total rainfall' ) x_txt = [str(i) for i in np.arange(-2.5, 3.6, 0.5)] x_txt.insert(0, '') x_txt.append('') ax.set_xlim([1.5, 15.5]) ax.set(xticks=np.arange(1.5, 15.5, 1), xticklabels=(x_txt)) ax.text(0.0, -0.15, 'Extreme dry', transform=ax.transAxes, fontsize=10, color=colors[0]) ax.text(0.25, -0.15, 'Moderate dry', transform=ax.transAxes, fontsize=10, color=colors[2]) ax.text(0.5, -0.15, 'Moderate wet', transform=ax.transAxes, fontsize=10, color=colors[6]) ax.text(0.8, -0.15, 'Extreme wet', transform=ax.transAxes, fontsize=10, color=colors[-1]) plt.subplots_adjust(bottom=0.15, top=0.9) plt.savefig('../figure/figure_heavyrainf_percent.pdf') print('Heavy rain precent figure saved')