import pandas as pd
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import netCDF4 as nc
import pandas as pd
import os
import csv
from glob import glob
import xarray as xr
import matplotlib.ticker as ticker
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.colors import LinearSegmentedColormap, Normalize
from mpl_toolkits.axes_grid1 import make_axes_locatable
import geopandas as gpd
import matplotlib.colors as mcolors
import matplotlib.lines as mlines
from matplotlib.font_manager import FontProperties
os.chdir('/Users/chenchenren/postdoc/paper/2N and water-US/Bootstrap/')
# Load data from an Excel file
excel_file = "soybean_warming_bootstrap.csv"
alldata = pd.read_csv(excel_file)
print(alldata)
geoid scenario id_cof tmp pre lnfer irrigation1 \ 0 1001 1.5 1 25.099590 6.254395 2.457656 0.377088 1 1001 1.5 2 25.099590 6.254395 2.457656 0.377088 2 1001 1.5 3 25.099590 6.254395 2.457656 0.377088 3 1001 1.5 4 25.099590 6.254395 2.457656 0.377088 4 1001 1.5 5 25.099590 6.254395 2.457656 0.377088 ... ... ... ... ... ... ... ... 3545995 55141 3.0 996 19.304302 6.116161 2.556201 NaN 3545996 55141 3.0 997 19.304302 6.116161 2.556201 NaN 3545997 55141 3.0 998 19.304302 6.116161 2.556201 NaN 3545998 55141 3.0 999 19.304302 6.116161 2.556201 NaN 3545999 55141 3.0 1000 19.304302 6.116161 2.556201 NaN y_temp_base_t y_temp_base2015 lnyield_obs har \ 0 7.474178 7.499539 7.509678 779.625 1 7.478996 7.505718 7.509678 779.625 2 7.607416 7.631373 7.509678 779.625 3 7.503553 7.528260 7.509678 779.625 4 7.474523 7.500269 7.509678 779.625 ... ... ... ... ... 3545995 8.220837 7.963717 7.921128 6700.507 3545996 8.204653 7.950847 7.921128 6700.507 3545997 8.228186 7.970028 7.921128 6700.507 3545998 8.238830 7.987553 7.921128 6700.507 3545999 8.256939 8.013674 7.921128 6700.507 gap_yield_warming gap_production_warming 0 -0.045718 -0.003564 1 -0.048138 -0.003753 2 -0.043216 -0.003369 3 -0.044553 -0.003473 4 -0.046403 -0.003618 ... ... ... 3545995 0.807729 0.541220 3545996 0.795947 0.533325 3545997 0.811432 0.543701 3545998 0.786978 0.527315 3545999 0.758709 0.508374 [3546000 rows x 13 columns]
ds15 = alldata[alldata['scenario'] == 1.5]
columns_to_sum = ['gap_production_warming']
t_warming15=ds15.groupby(['id_cof'])[columns_to_sum].sum().reset_index()
print(t_warming15)
p5_15 = np.percentile(t_warming15['gap_production_warming'], 5)
p95_15=np.percentile(t_warming15['gap_production_warming'], 95)
ds3 = alldata[alldata['scenario'] == 3]
columns_to_sum = ['gap_production_warming']
t_warming3=ds3.groupby(['id_cof'])[columns_to_sum].sum().reset_index()
print(t_warming3)
p5_3 = np.percentile(t_warming3['gap_production_warming'], 5)
p95_3=np.percentile(t_warming3['gap_production_warming'], 95)
print(p5_15,p95_15,p5_3,p95_3)
id_cof gap_production_warming 0 1 13.473122 1 2 28.872242 2 3 43.721684 3 4 53.893859 4 5 44.279783 .. ... ... 995 996 39.122623 996 997 56.674943 997 998 52.445357 998 999 25.481040 999 1000 31.237969 [1000 rows x 2 columns] id_cof gap_production_warming 0 1 -263.818531 1 2 -162.861102 2 3 -122.852100 3 4 -68.061741 4 5 -149.794360 .. ... ... 995 996 -137.627017 996 997 -84.010077 997 998 -119.289528 998 999 -224.171006 999 1000 -174.697736 [1000 rows x 2 columns] 23.417620024572543 60.96475656164288 -196.159626161072 -55.87372562472601
data = alldata[alldata['scenario'] == 1.5]
# 5% percentile
data['gap_production_warming'] = data.groupby(['geoid'])['gap_production_warming'].transform(lambda x: np.percentile(x, 5))
data=data.drop_duplicates(subset=['geoid'])
print(data)
print(np.min(data['gap_production_warming']),np.max(data['gap_production_warming']),np.mean(data['gap_production_warming']))
print(np.percentile(data['gap_production_warming'], 5),np.percentile(data['gap_production_warming'], 95))
# Replace values in 'gap_yield' greater than p90 with p90
data.loc[data['gap_production_warming'] > 2.1, 'gap_production_warming'] = 2.1
data.loc[data['gap_production_warming'] < -2.1, 'gap_production_warming'] = -2.1
data['geoid'] = data['geoid'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in year_2020_data
data.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data' DataFrame to 'object' data type
data['GEOID'] = data['GEOID'].astype(float)
# Now, merge 'data' with county boundaries based on 'GEOID' attribute
merged_data = gdf_county.merge(data, on='GEOID', how='left')
# Filter the data to include only geometries within the specified longitude and latitude range
filtered_data = merged_data.cx[-130:-60, 25:50]
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.Spectral(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-2.2, vmax=2.2)
# Plot both county boundaries and 'y_temp' values on the same map within the specified range
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey') # Plot county boundaries in grey
sc = plt.scatter([], [], c=[], cmap=cmap, norm=norm) # Create an empty ScalarMappable for the colorbar
# Define custom colorbar ticks
custom_ticks = [-2, -1, 0, 1, 2]
# Adjust the height of the colorbar by setting the shrink parameter
cbar = plt.colorbar(sc, ax=ax, label='Production change\n($10^{4}$ t)', orientation='vertical', pad=0.02, shrink=0.8, ticks=custom_ticks)
# Set the font size of the colorbar label
# Set the colorbar label text, font size, and padding
cbar.ax.set_ylabel('Production change\n($10^{4}$ tonnes/yr)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16) # Adjust tick label fontsize
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
filtered_data.plot(column='gap_production_warming', cmap=cmap, ax=ax, norm=norm)
# Add labels, titles, or other plot configurations as needed
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.2, '(c) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_all_production_15_p5.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/2113936764.py:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['gap_production_warming'] = data.groupby(['geoid'])['gap_production_warming'].transform(lambda x: np.percentile(x, 5))
geoid scenario id_cof tmp pre lnfer irrigation1 \ 0 1001 1.5 1 25.099590 6.254395 2.457656 0.377088 2000 1003 1.5 1 26.261318 9.274925 1.292553 0.738441 4000 1005 1.5 1 25.408514 6.727813 2.114730 0.028216 6000 1009 1.5 1 23.976584 6.790346 4.361742 0.056342 8000 1011 1.5 1 25.246212 6.448847 4.858469 0.030586 ... ... ... ... ... ... ... ... 3536000 55133 1.5 1 18.514920 5.639188 1.516143 NaN 3538000 55135 1.5 1 17.319042 5.592127 1.466300 NaN 3540000 55137 1.5 1 17.765668 5.555818 1.553310 NaN 3542000 55139 1.5 1 18.099080 5.372444 1.255133 NaN 3544000 55141 1.5 1 17.398562 5.719637 2.556201 NaN y_temp_base_t y_temp_base2015 lnyield_obs har \ 0 7.474178 7.499539 7.509678 779.625 2000 7.197338 7.207396 7.927428 6358.433 4000 7.428942 7.443278 8.161582 526.500 6000 7.889409 7.925261 7.840701 1107.736 8000 7.808706 7.827184 7.984614 870.750 ... ... ... ... ... 3536000 7.954243 7.917639 7.974899 7837.872 3538000 7.876120 7.832488 7.957613 9909.051 3540000 7.910403 7.871683 7.941248 5335.875 3542000 7.894186 7.865316 8.028848 14541.420 3544000 7.966143 7.894034 7.921128 6700.507 gap_yield_warming gap_production_warming 0 -0.045718 -0.003727 2000 -0.027743 -0.019340 4000 -0.049872 -0.002605 6000 -0.089522 -0.009786 8000 -0.053741 -0.004705 ... ... ... 3536000 0.108381 0.083619 3538000 0.127429 0.116701 3540000 0.110971 0.055787 3542000 0.089869 0.121146 3544000 0.205987 0.135636 [1773 rows x 13 columns] -3.3181771942742713 3.053110777122164 0.005905536688759566 -0.43960474399223093 0.5036116026289954
data = alldata[alldata['scenario'] == 1.5]
# 95% percentile
data['gap_production_warming'] = data.groupby(['geoid'])['gap_production_warming'].transform(lambda x: np.percentile(x, 95))
data=data.drop_duplicates(subset=['geoid'])
print(data)
print(np.min(data['gap_production_warming']),np.max(data['gap_production_warming']),np.mean(data['gap_production_warming']))
print(np.percentile(data['gap_production_warming'], 5),np.percentile(data['gap_production_warming'], 95))
# Replace values in 'gap_yield' greater than p90 with p90
data.loc[data['gap_production_warming'] > 2.1, 'gap_production_warming'] = 2.1
data.loc[data['gap_production_warming'] < -2.1, 'gap_production_warming'] = -2.1
data['geoid'] = data['geoid'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in year_2020_data
data.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data' DataFrame to 'object' data type
data['GEOID'] = data['GEOID'].astype(float)
# Now, merge 'data' with county boundaries based on 'GEOID' attribute
merged_data = gdf_county.merge(data, on='GEOID', how='left')
# Filter the data to include only geometries within the specified longitude and latitude range
filtered_data = merged_data.cx[-130:-60, 25:50]
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.Spectral(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-2.2, vmax=2.2)
# Plot both county boundaries and 'y_temp' values on the same map within the specified range
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey') # Plot county boundaries in grey
sc = plt.scatter([], [], c=[], cmap=cmap, norm=norm) # Create an empty ScalarMappable for the colorbar
# Define custom colorbar ticks
custom_ticks = [-2, -1, 0, 1, 2]
# Adjust the height of the colorbar by setting the shrink parameter
cbar = plt.colorbar(sc, ax=ax, label='Production change\n($10^{4}$ t)', orientation='vertical', pad=0.02, shrink=0.8, ticks=custom_ticks)
# Set the font size of the colorbar label
# Set the colorbar label text, font size, and padding
cbar.ax.set_ylabel('Production change\n($10^{4}$ tonnes/yr)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16) # Adjust tick label fontsize
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
filtered_data.plot(column='gap_production_warming', cmap=cmap, ax=ax, norm=norm)
# Add labels, titles, or other plot configurations as needed
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.2, '(d) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_all_production_15_p95.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/2192528739.py:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['gap_production_warming'] = data.groupby(['geoid'])['gap_production_warming'].transform(lambda x: np.percentile(x, 95))
geoid scenario id_cof tmp pre lnfer irrigation1 \ 0 1001 1.5 1 25.099590 6.254395 2.457656 0.377088 2000 1003 1.5 1 26.261318 9.274925 1.292553 0.738441 4000 1005 1.5 1 25.408514 6.727813 2.114730 0.028216 6000 1009 1.5 1 23.976584 6.790346 4.361742 0.056342 8000 1011 1.5 1 25.246212 6.448847 4.858469 0.030586 ... ... ... ... ... ... ... ... 3536000 55133 1.5 1 18.514920 5.639188 1.516143 NaN 3538000 55135 1.5 1 17.319042 5.592127 1.466300 NaN 3540000 55137 1.5 1 17.765668 5.555818 1.553310 NaN 3542000 55139 1.5 1 18.099080 5.372444 1.255133 NaN 3544000 55141 1.5 1 17.398562 5.719637 2.556201 NaN y_temp_base_t y_temp_base2015 lnyield_obs har \ 0 7.474178 7.499539 7.509678 779.625 2000 7.197338 7.207396 7.927428 6358.433 4000 7.428942 7.443278 8.161582 526.500 6000 7.889409 7.925261 7.840701 1107.736 8000 7.808706 7.827184 7.984614 870.750 ... ... ... ... ... 3536000 7.954243 7.917639 7.974899 7837.872 3538000 7.876120 7.832488 7.957613 9909.051 3540000 7.910403 7.871683 7.941248 5335.875 3542000 7.894186 7.865316 8.028848 14541.420 3544000 7.966143 7.894034 7.921128 6700.507 gap_yield_warming gap_production_warming 0 -0.045718 -0.003395 2000 -0.027743 -0.012741 4000 -0.049872 -0.002410 6000 -0.089522 -0.008822 8000 -0.053741 -0.004421 ... ... ... 3536000 0.108381 0.091845 3538000 0.127429 0.132777 3540000 0.110971 0.063482 3542000 0.089869 0.140061 3544000 0.205987 0.153914 [1773 rows x 13 columns] -3.046997739875447 3.3724094315404294 0.042290714269416 -0.3787535313120097 0.5770476979433069
data = alldata[alldata['scenario'] == 3.0]
# 5% percentile
data['gap_production_warming'] = data.groupby(['geoid'])['gap_production_warming'].transform(lambda x: np.percentile(x, 5))
data=data.drop_duplicates(subset=['geoid'])
print(data)
print(np.min(data['gap_production_warming']),np.max(data['gap_production_warming']),np.mean(data['gap_production_warming']))
print(np.percentile(data['gap_production_warming'], 5),np.percentile(data['gap_production_warming'], 95))
# Replace values in 'gap_yield' greater than p90 with p90
data.loc[data['gap_production_warming'] > 2.1, 'gap_production_warming'] = 2.1
data.loc[data['gap_production_warming'] < -2.1, 'gap_production_warming'] = -2.1
data['geoid'] = data['geoid'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in year_2020_data
data.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data' DataFrame to 'object' data type
data['GEOID'] = data['GEOID'].astype(float)
# Now, merge 'data' with county boundaries based on 'GEOID' attribute
merged_data = gdf_county.merge(data, on='GEOID', how='left')
# Filter the data to include only geometries within the specified longitude and latitude range
filtered_data = merged_data.cx[-130:-60, 25:50]
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.Spectral(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-2.2, vmax=2.2)
# Plot both county boundaries and 'y_temp' values on the same map within the specified range
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey') # Plot county boundaries in grey
sc = plt.scatter([], [], c=[], cmap=cmap, norm=norm) # Create an empty ScalarMappable for the colorbar
# Define custom colorbar ticks
custom_ticks = [-2, -1, 0, 1, 2]
# Adjust the height of the colorbar by setting the shrink parameter
cbar = plt.colorbar(sc, ax=ax, label='Production change\n($10^{4}$ t)', orientation='vertical', pad=0.02, shrink=0.8, ticks=custom_ticks)
# Set the font size of the colorbar label
# Set the colorbar label text, font size, and padding
cbar.ax.set_ylabel('Production change\n($10^{4}$ tonnes/yr)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16) # Adjust tick label fontsize
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
filtered_data.plot(column='gap_production_warming', cmap=cmap, ax=ax, norm=norm)
# Add labels, titles, or other plot configurations as needed
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.2, '(g) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_all_production_3_p5.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/3359553665.py:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['gap_production_warming'] = data.groupby(['geoid'])['gap_production_warming'].transform(lambda x: np.percentile(x, 5))
geoid scenario id_cof tmp pre lnfer irrigation1 \ 1000 1001 3.0 1 26.784940 6.208599 2.457656 0.377088 3000 1003 3.0 1 27.808318 8.926058 1.292553 0.738441 5000 1005 3.0 1 27.067738 6.742623 2.114730 0.028216 7000 1009 3.0 1 25.706190 6.658286 4.361742 0.056342 9000 1011 3.0 1 26.923034 6.491163 4.858469 0.030586 ... ... ... ... ... ... ... ... 3537000 55133 3.0 1 20.450390 5.862562 1.516143 NaN 3539000 55135 3.0 1 19.245958 5.878897 1.466300 NaN 3541000 55137 3.0 1 19.677776 5.922441 1.553310 NaN 3543000 55139 3.0 1 20.012748 5.680057 1.255133 NaN 3545000 55141 3.0 1 19.304302 6.116161 2.556201 NaN y_temp_base_t y_temp_base2015 lnyield_obs har \ 1000 7.232958 7.499539 7.509678 779.625 3000 6.940542 7.207396 7.927428 6358.433 5000 7.176762 7.443278 8.161582 526.500 7000 7.688125 7.925261 7.840701 1107.736 9000 7.563009 7.827184 7.984614 870.750 ... ... ... ... ... 3537000 8.024770 7.917639 7.974899 7837.872 3539000 7.991792 7.832488 7.957613 9909.051 3541000 8.018031 7.871683 7.941248 5335.875 3543000 7.973059 7.865316 8.028848 14541.420 3545000 8.140930 7.894034 7.921128 6700.507 gap_yield_warming gap_production_warming 1000 -0.427207 -0.033176 3000 -0.649310 -0.412525 5000 -0.819719 -0.042710 7000 -0.536654 -0.058469 9000 -0.681495 -0.058741 ... ... ... 3537000 0.328728 0.264432 3539000 0.493431 0.477470 3541000 0.442990 0.234946 3543000 0.349044 0.506301 3545000 0.771492 0.517581 [1773 rows x 13 columns] -8.18882744115128 10.00324153276105 -0.13323164114374003 -1.791900038071304 1.589146594370037
data = alldata[alldata['scenario'] == 3.0]
# 95% percentile
data['gap_production_warming'] = data.groupby(['geoid'])['gap_production_warming'].transform(lambda x: np.percentile(x, 95))
data=data.drop_duplicates(subset=['geoid'])
print(data)
print(np.min(data['gap_production_warming']),np.max(data['gap_production_warming']),np.mean(data['gap_production_warming']))
print(np.percentile(data['gap_production_warming'], 5),np.percentile(data['gap_production_warming'], 95))
# Replace values in 'gap_yield' greater than p90 with p90
data.loc[data['gap_production_warming'] > 2.1, 'gap_production_warming'] = 2.1
data.loc[data['gap_production_warming'] < -2.1, 'gap_production_warming'] = -2.1
data['geoid'] = data['geoid'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in year_2020_data
data.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data' DataFrame to 'object' data type
data['GEOID'] = data['GEOID'].astype(float)
# Now, merge 'data' with county boundaries based on 'GEOID' attribute
merged_data = gdf_county.merge(data, on='GEOID', how='left')
# Filter the data to include only geometries within the specified longitude and latitude range
filtered_data = merged_data.cx[-130:-60, 25:50]
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.Spectral(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-2.2, vmax=2.2)
# Plot both county boundaries and 'y_temp' values on the same map within the specified range
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey') # Plot county boundaries in grey
sc = plt.scatter([], [], c=[], cmap=cmap, norm=norm) # Create an empty ScalarMappable for the colorbar
# Define custom colorbar ticks
custom_ticks = [-2, -1, 0, 1, 2]
# Adjust the height of the colorbar by setting the shrink parameter
cbar = plt.colorbar(sc, ax=ax, label='Production change\n($10^{4}$ t)', orientation='vertical', pad=0.02, shrink=0.8, ticks=custom_ticks)
# Set the font size of the colorbar label
# Set the colorbar label text, font size, and padding
cbar.ax.set_ylabel('Production change\n($10^{4}$ tonnes/yr)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16) # Adjust tick label fontsize
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
filtered_data.plot(column='gap_production_warming', cmap=cmap, ax=ax, norm=norm)
# Add labels, titles, or other plot configurations as needed
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.2, '(h) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_all_production_3_p95.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/3050137360.py:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['gap_production_warming'] = data.groupby(['geoid'])['gap_production_warming'].transform(lambda x: np.percentile(x, 95))
geoid scenario id_cof tmp pre lnfer irrigation1 \ 1000 1001 3.0 1 26.784940 6.208599 2.457656 0.377088 3000 1003 3.0 1 27.808318 8.926058 1.292553 0.738441 5000 1005 3.0 1 27.067738 6.742623 2.114730 0.028216 7000 1009 3.0 1 25.706190 6.658286 4.361742 0.056342 9000 1011 3.0 1 26.923034 6.491163 4.858469 0.030586 ... ... ... ... ... ... ... ... 3537000 55133 3.0 1 20.450390 5.862562 1.516143 NaN 3539000 55135 3.0 1 19.245958 5.878897 1.466300 NaN 3541000 55137 3.0 1 19.677776 5.922441 1.553310 NaN 3543000 55139 3.0 1 20.012748 5.680057 1.255133 NaN 3545000 55141 3.0 1 19.304302 6.116161 2.556201 NaN y_temp_base_t y_temp_base2015 lnyield_obs har \ 1000 7.232958 7.499539 7.509678 779.625 3000 6.940542 7.207396 7.927428 6358.433 5000 7.176762 7.443278 8.161582 526.500 7000 7.688125 7.925261 7.840701 1107.736 9000 7.563009 7.827184 7.984614 870.750 ... ... ... ... ... 3537000 8.024770 7.917639 7.974899 7837.872 3539000 7.991792 7.832488 7.957613 9909.051 3541000 8.018031 7.871683 7.941248 5335.875 3543000 7.973059 7.865316 8.028848 14541.420 3545000 8.140930 7.894034 7.921128 6700.507 gap_yield_warming gap_production_warming 1000 -0.427207 -0.029788 3000 -0.649310 -0.365008 5000 -0.819719 -0.038123 7000 -0.536654 -0.052174 9000 -0.681495 -0.052524 ... ... ... 3537000 0.328728 0.292238 3539000 0.493431 0.521319 3541000 0.442990 0.256656 3543000 0.349044 0.560604 3545000 0.771492 0.570866 [1773 rows x 13 columns] -7.265671193943158 10.980283745965528 -0.008914924188189483 -1.5268892810384909 1.8833542334218545
excel_file = "cost_benefit_county_bootstrap.xlsx"
sheet_name = "soybean"
data = pd.read_excel(excel_file, sheet_name=sheet_name)
data.loc[(data['net_benefit_mana_p5'] > 4), 'net_benefit_mana_p5'] = 4
data.loc[(data['net_benefit_mana_p5'] < -4), 'net_benefit_mana_p5'] = -4
data.loc[(data['net_benefit_mana_p95'] > 4), 'net_benefit_mana_p95'] = 4
data.loc[(data['net_benefit_mana_p95'] < -4), 'net_benefit_mana_p95'] = -4
data.loc[(data['net_benefit_lnfer_p5'] > 4), 'net_benefit_lnfer_p5'] = 4
data.loc[(data['net_benefit_lnfer_p5'] < -4), 'net_benefit_lnfer_p5'] = -4
data.loc[(data['net_benefit_lnfer_p95'] > 4), 'net_benefit_lnfer_p95'] = 4
data.loc[(data['net_benefit_lnfer_p95'] < -4), 'net_benefit_lnfer_p95'] = -4
data.loc[(data['net_benefit_irri_p5'] > 4), 'net_benefit_irri_p5'] = 4
data.loc[(data['net_benefit_irri_p5'] < -4), 'net_benefit_irri_p5'] = -4
data.loc[(data['net_benefit_irri_p95'] > 4), 'net_benefit_irri_p95'] = 4
data.loc[(data['net_benefit_irri_p95'] < -4), 'net_benefit_irri_p95'] = -4
data15 = data[data['scenario'] == 1.5]
data15['geoid'] = data15['geoid'].astype(float)
print(data15)
data3 = data[data['scenario'] == 3.0]
data3['geoid'] = data3['geoid'].astype(float)
print(data3)
geoid scenario production_benefit_irri_p95 \ 0 1001.0 1.5 -0.008377 2 1003.0 1.5 0.000000 4 1005.0 1.5 -0.018896 5 1009.0 1.5 -0.111773 6 1011.0 1.5 -0.095638 ... ... ... ... 3386 55133.0 1.5 0.342804 3388 55135.0 1.5 0.492121 3390 55137.0 1.5 0.235326 3392 55139.0 1.5 0.517853 3394 55141.0 1.5 0.569354 production_benefit_irri_p5 production_benefit_irri_med \ 0 -0.013282 -0.010858 2 0.000000 0.000000 4 -0.030174 -0.024385 5 -0.146155 -0.129706 6 -0.136132 -0.117676 ... ... ... 3386 0.322517 0.332130 3388 0.453025 0.472646 3390 0.216555 0.225801 3392 0.472253 0.494511 3394 0.525596 0.547686 production_benefit_lnfer_p95 production_benefit_lnfer_p5 \ 0 0.014337 0.013507 2 0.070591 0.054278 4 0.010018 0.009510 5 0.037359 0.035023 6 0.018197 0.017422 ... ... ... 3386 0.342804 0.322517 3388 0.492121 0.453025 3390 0.235326 0.216555 3392 0.517853 0.472253 3394 0.569354 0.525596 production_benefit_lnfer_med production_benefit_mana_p95 \ 0 0.013907 0.017189 2 0.062423 0.100771 4 0.009765 0.012577 5 0.036220 0.038670 6 0.017808 0.018535 ... ... ... 3386 0.332130 0.342804 3388 0.472646 0.492121 3390 0.225801 0.235326 3392 0.494511 0.517853 3394 0.547686 0.569354 production_benefit_mana_p5 ... fer_cost_mana_med \ 0 0.014351 ... 0.003868 2 0.062857 ... 0.005041 4 0.011626 ... 0.000837 5 0.036173 ... 0.044621 6 0.017731 ... 0.028847 ... ... ... ... 3386 0.322517 ... 0.000000 3388 0.453025 ... 0.000000 3390 0.216555 ... 0.000000 3392 0.472253 ... 0.000000 3394 0.525596 ... 0.000000 net_benefit_irri_p95 net_benefit_irri_p5 net_benefit_irri_med \ 0 -0.139827 -0.144732 -0.142307 2 0.844031 0.401571 0.550610 4 -0.174939 -0.186217 -0.180428 5 -0.428671 -0.463054 -0.446604 6 -0.352953 -0.393447 -0.374991 ... ... ... ... 3386 0.342804 0.322517 0.332130 3388 0.492121 0.453025 0.472646 3390 0.235326 0.216555 0.225801 3392 0.517853 0.472253 0.494511 3394 0.569354 0.525596 0.547686 net_benefit_lnfer_p95 net_benefit_lnfer_p5 net_benefit_lnfer_med \ 0 0.010364 0.009723 0.010049 2 0.065083 0.049979 0.057282 4 0.009162 0.008694 0.008930 5 -0.006675 -0.010412 -0.008430 6 -0.009948 -0.012264 -0.011035 ... ... ... ... 3386 0.342804 0.322517 0.332130 3388 0.492121 0.453025 0.472646 3390 0.235326 0.216555 0.225801 3392 0.517853 0.472253 0.494511 3394 0.569354 0.525596 0.547686 net_benefit_mana_p95 net_benefit_mana_p5 net_benefit_mana_med 0 0.120987 0.118221 0.119644 2 1.815043 1.777676 1.795156 4 0.017206 0.016193 0.016682 5 -0.005456 -0.009165 -0.007186 6 0.000121 -0.002195 -0.000965 ... ... ... ... 3386 0.342804 0.322517 0.332130 3388 0.492121 0.453025 0.472646 3390 0.235326 0.216555 0.225801 3392 0.517853 0.472253 0.494511 3394 0.569354 0.525596 0.547686 [1701 rows x 32 columns] geoid scenario production_benefit_irri_p95 \ 1 1001.0 3.0 -0.006339 3 1003.0 3.0 -0.011550 8 1015.0 3.0 0.000000 10 1019.0 3.0 -0.140458 12 1021.0 3.0 -0.017565 ... ... ... ... 3387 55133.0 3.0 1.088153 3389 55135.0 3.0 1.947926 3391 55137.0 3.0 0.958702 3393 55139.0 3.0 2.087004 3395 55141.0 3.0 2.128137 production_benefit_irri_p5 production_benefit_irri_med \ 1 -0.011297 -0.008745 3 -0.027001 -0.019200 8 0.000000 0.000000 10 -0.205430 -0.173297 12 -0.026884 -0.022625 ... ... ... 3387 1.018382 1.053084 3389 1.840708 1.891951 3391 0.905106 0.930787 3393 1.953403 2.017032 3395 1.991517 2.061796 production_benefit_lnfer_p95 production_benefit_lnfer_p5 \ 1 0.126910 0.118348 3 1.574879 1.454679 8 0.166735 0.151677 10 0.759270 0.697610 12 0.098019 0.091467 ... ... ... 3387 1.088153 1.018382 3389 1.947926 1.840708 3391 0.958702 0.905106 3393 2.087004 1.953403 3395 2.128137 1.991517 production_benefit_lnfer_med production_benefit_mana_p95 \ 1 0.122731 0.164375 3 1.516178 2.068451 8 0.159471 0.196438 10 0.729055 0.905763 12 0.094877 0.125651 ... ... ... 3387 1.053084 1.088153 3389 1.891951 1.947926 3391 0.930787 0.958702 3393 2.017032 2.087004 3395 2.061796 2.128137 production_benefit_mana_p5 ... fer_cost_mana_med \ 1 0.150293 ... 0.110736 3 1.860377 ... 0.726592 8 0.175868 ... 0.177372 10 0.819368 ... 0.392755 12 0.115084 ... 0.076355 ... ... ... ... 3387 1.018382 ... 0.000000 3389 1.840708 ... 0.000000 3391 0.905106 ... 0.000000 3393 1.953403 ... 0.000000 3395 1.991517 ... 0.000000 net_benefit_irri_p95 net_benefit_irri_p5 net_benefit_irri_med \ 1 -0.137789 -0.142747 -0.140195 3 -0.242124 -0.257575 -0.249773 8 0.006211 0.006211 0.006211 10 -1.343877 -1.408848 -1.376715 12 -0.128380 -0.137700 -0.133440 ... ... ... ... 3387 1.088153 1.018382 1.053084 3389 1.947926 1.840708 1.891951 3391 0.958702 0.905106 0.930787 3393 2.087004 1.953403 2.017032 3395 2.128137 1.991517 2.061796 net_benefit_lnfer_p95 net_benefit_lnfer_p5 net_benefit_lnfer_med \ 1 0.024494 -0.003166 0.011742 3 0.970720 0.495534 0.790400 8 0.011925 -0.054324 -0.017679 10 0.360197 0.306292 0.334837 12 0.024282 0.011321 0.018255 ... ... ... ... 3387 1.088153 1.018382 1.053084 3389 1.947926 1.840708 1.891951 3391 0.958702 0.905106 0.930787 3393 2.087004 1.953403 2.017032 3395 2.128137 1.991517 2.061796 net_benefit_mana_p95 net_benefit_mana_p5 net_benefit_mana_med 1 0.058078 0.032396 0.046347 3 3.141204 2.665833 2.959619 8 0.445172 0.380364 0.416163 10 0.497402 0.436963 0.468057 12 0.059104 0.046981 0.053608 ... ... ... ... 3387 1.088153 1.018382 1.053084 3389 1.947926 1.840708 1.891951 3391 0.958702 0.905106 0.930787 3393 2.087004 1.953403 2.017032 3395 2.128137 1.991517 2.061796 [1695 rows x 32 columns]
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/2774632440.py:19: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15['geoid'] = data15['geoid'].astype(float) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/2774632440.py:23: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3['geoid'] = data3['geoid'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data15 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data15' Data15Frame
data15.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data15' Data15Frame to 'object' data15 type
data15['GEOID'] = data15['GEOID'].astype(float)
# Merge 'data15' with county boundaries based on 'GEOID' attribute
merged_data15 = gdf_county.merge(data15, on='GEOID', how='left')
# Filter the data15 to include only geometries within the specified longitude and latitude range
filtered_data15 = merged_data15.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data15
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_irrigation values
filtered_data15.plot(column='net_benefit_irri_p5', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(d) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_irri_15_p5.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/3332761606.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/3332761606.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15['GEOID'] = data15['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data15 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data15' Data15Frame
data15.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data15' Data15Frame to 'object' data15 type
data15['GEOID'] = data15['GEOID'].astype(float)
# Merge 'data15' with county boundaries based on 'GEOID' attribute
merged_data15 = gdf_county.merge(data15, on='GEOID', how='left')
# Filter the data15 to include only geometries within the specified longitude and latitude range
filtered_data15 = merged_data15.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data15
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_irrigation values
filtered_data15.plot(column='net_benefit_irri_p95', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(d) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_irri_15_p95.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/1578514162.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/1578514162.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15['GEOID'] = data15['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data3 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data3' Data3Frame
data3.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data3' Data3Frame to 'object' data3 type
data3['GEOID'] = data3['GEOID'].astype(float)
# Merge 'data3' with county boundaries based on 'GEOID' attribute
merged_data3 = gdf_county.merge(data3, on='GEOID', how='left')
# Filter the data3 to include only geometries within the specified longitude and latitude range
filtered_data3 = merged_data3.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data3
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_irrigation values
filtered_data3.plot(column='net_benefit_irri_p5', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(j) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_irri_3_p5.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/4239337382.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/4239337382.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3['GEOID'] = data3['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data3 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data3' Data3Frame
data3.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data3' Data3Frame to 'object' data3 type
data3['GEOID'] = data3['GEOID'].astype(float)
# Merge 'data3' with county boundaries based on 'GEOID' attribute
merged_data3 = gdf_county.merge(data3, on='GEOID', how='left')
# Filter the data3 to include only geometries within the specified longitude and latitude range
filtered_data3 = merged_data3.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data3
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_irrigation values
filtered_data3.plot(column='net_benefit_irri_p95', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(j) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_irri_3_p95.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/1467272273.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/1467272273.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3['GEOID'] = data3['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data15 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data15' Data15Frame
data15.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data15' Data15Frame to 'object' data15 type
data15['GEOID'] = data15['GEOID'].astype(float)
# Merge 'data15' with county boundaries based on 'GEOID' attribute
merged_data15 = gdf_county.merge(data15, on='GEOID', how='left')
# Filter the data15 to include only geometries within the specified longitude and latitude range
filtered_data15 = merged_data15.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data15
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_fergation values
filtered_data15.plot(column='net_benefit_lnfer_p5', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(e) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_fer_15_p5.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/2464012243.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/2464012243.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15['GEOID'] = data15['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data15 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data15' Data15Frame
data15.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data15' Data15Frame to 'object' data15 type
data15['GEOID'] = data15['GEOID'].astype(float)
# Merge 'data15' with county boundaries based on 'GEOID' attribute
merged_data15 = gdf_county.merge(data15, on='GEOID', how='left')
# Filter the data15 to include only geometries within the specified longitude and latitude range
filtered_data15 = merged_data15.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data15
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_fergation values
filtered_data15.plot(column='net_benefit_lnfer_p95', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(e) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_fer_15_p95.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/2182512367.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/2182512367.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15['GEOID'] = data15['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data3 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data3' Data3Frame
data3.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data3' Data3Frame to 'object' data3 type
data3['GEOID'] = data3['GEOID'].astype(float)
# Merge 'data3' with county boundaries based on 'GEOID' attribute
merged_data3 = gdf_county.merge(data3, on='GEOID', how='left')
# Filter the data3 to include only geometries within the specified longitude and latitude range
filtered_data3 = merged_data3.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data3
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_fergation values
filtered_data3.plot(column='net_benefit_lnfer_p5', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(k) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_fer_3_p5.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/3166943015.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/3166943015.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3['GEOID'] = data3['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data3 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data3' Data3Frame
data3.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data3' Data3Frame to 'object' data3 type
data3['GEOID'] = data3['GEOID'].astype(float)
# Merge 'data3' with county boundaries based on 'GEOID' attribute
merged_data3 = gdf_county.merge(data3, on='GEOID', how='left')
# Filter the data3 to include only geometries within the specified longitude and latitude range
filtered_data3 = merged_data3.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data3
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_fergation values
filtered_data3.plot(column='net_benefit_lnfer_p95', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(k) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_fer_3_p95.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/4104011297.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/4104011297.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3['GEOID'] = data3['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data15 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data15' Data15Frame
data15.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data15' Data15Frame to 'object' data15 type
data15['GEOID'] = data15['GEOID'].astype(float)
# Merge 'data15' with county boundaries based on 'GEOID' attribute
merged_data15 = gdf_county.merge(data15, on='GEOID', how='left')
# Filter the data15 to include only geometries within the specified longitude and latitude range
filtered_data15 = merged_data15.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data15
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_managation values
filtered_data15.plot(column='net_benefit_mana_p5', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(f) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_mana_15_p5.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/763855233.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/763855233.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15['GEOID'] = data15['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data15 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data15' Data15Frame
data15.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data15' Data15Frame to 'object' data15 type
data15['GEOID'] = data15['GEOID'].astype(float)
# Merge 'data15' with county boundaries based on 'GEOID' attribute
merged_data15 = gdf_county.merge(data15, on='GEOID', how='left')
# Filter the data15 to include only geometries within the specified longitude and latitude range
filtered_data15 = merged_data15.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data15
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_managation values
filtered_data15.plot(column='net_benefit_mana_p95', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(f) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_mana_15_p95.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/1920273409.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/1920273409.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data15['GEOID'] = data15['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data3 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data3' Data3Frame
data3.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data3' Data3Frame to 'object' data3 type
data3['GEOID'] = data3['GEOID'].astype(float)
# Merge 'data3' with county boundaries based on 'GEOID' attribute
merged_data3 = gdf_county.merge(data3, on='GEOID', how='left')
# Filter the data3 to include only geometries within the specified longitude and latitude range
filtered_data3 = merged_data3.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data3
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_managation values
filtered_data3.plot(column='net_benefit_mana_p5', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(l) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_mana_3_p5.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/4292222005.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/4292222005.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3['GEOID'] = data3['GEOID'].astype(float)
# Load US county boundaries shapefile using geopandas
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_county_20m/surface_shapefile.shp'
gdf_county = gpd.read_file(shapefile_path)
# Ensure 'GEOID' column in the shapefile has the 'object' data3 type
gdf_county['GEOID'] = gdf_county['GEOID'].astype(float)
# Rename the 'geoid' column to 'GEOID' in the 'data3' Data3Frame
data3.rename(columns={'geoid': 'GEOID'}, inplace=True)
# Convert the 'GEOID' column in the 'data3' Data3Frame to 'object' data3 type
data3['GEOID'] = data3['GEOID'].astype(float)
# Merge 'data3' with county boundaries based on 'GEOID' attribute
merged_data3 = gdf_county.merge(data3, on='GEOID', how='left')
# Filter the data3 to include only geometries within the specified longitude and latitude range
filtered_data3 = merged_data3.cx[-130:-60, 25:50]
# Plot the county boundaries
fig, ax = plt.subplots(figsize=(10, 4))
shapefile_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Data/cb_2018_us_state_20m/cb_2018_us_state_20m.shp'
gdf_state = gpd.read_file(shapefile_path)
gdf_state.rename(columns={'GEOID': 'GEOID_state'}, inplace=True)
gdf_state = gdf_state.cx[-130:-60, 25:50]
gdf_state.boundary.plot(ax=ax, linewidth=0.3, color='grey')
# Create a ScalarMappable without plotting the data3
cmap = mcolors.ListedColormap(['white'] + list(plt.cm.PiYG(np.linspace(0, 1, 255))))
norm = mcolors.Normalize(vmin=-4.2, vmax=4.2)
sc = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sc.set_array([])
# Add colorbar
custom_ticks = [-4, -2, 0, 2, 4]
cbar = plt.colorbar(sc, ax=ax, label='Net benefits (Million $)', orientation='vertical', pad=0.02, shrink=1, ticks=custom_ticks)
cbar.ax.set_ylabel('Net benefits (Million $)', fontsize=16, fontfamily='Arial')
cbar.ax.tick_params(labelsize=16)
cbar.ax.set_yticklabels(custom_ticks, fontsize=16, fontfamily='Arial')
# Plot the mean_gap_managation values
filtered_data3.plot(column='net_benefit_mana_p95', cmap=cmap, ax=ax, norm=norm)
# Set font properties for the legend
font_prop = FontProperties(family='Arial', size=16)
font_prop.set_weight('bold')
# Additional plot configurations
ax.set_xticks([]) # Remove x-axis ticks
ax.set_yticks([]) # Remove y-axis ticks
ax.set_frame_on(False) # Turn off the frame
plt.text(0.25, 0.22, '(l) Soybean', transform=plt.gcf().transFigure,
fontsize=18, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_mana_3_p95.png', format='png',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/3660707111.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3.rename(columns={'geoid': 'GEOID'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_61041/3660707111.py:12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data3['GEOID'] = data3['GEOID'].astype(float)