import pandas as pd
import numpy as np
import statsmodels.api as sm
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 tifffile
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 geotiff import GeoTiff
from rasterio import open as rasopen
import matplotlib.cm as cm
from PIL import Image
from matplotlib.font_manager import FontProperties
/Users/chenchenren/anaconda3/lib/python3.11/site-packages/pandas/core/arrays/masked.py:60: UserWarning: Pandas requires version '1.3.6' or newer of 'bottleneck' (version '1.3.5' currently installed). from pandas.core import (
os.chdir('/Users/chenchenren/postdoc/paper/2N and water-US/Figure 7 Cost and Benefit/')
excel_file = "soybean_cost_benefit_county.csv"
data = pd.read_csv(excel_file)
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 production_benefit_lnfer \ 1 20033.0 1.5 0.010457 0.077034 3 20119.0 1.5 0.000000 0.508782 5 20097.0 1.5 0.000000 0.430507 7 20047.0 1.5 0.000000 0.750119 9 20057.0 1.5 0.000000 0.255920 ... ... ... ... ... 3536 55133.0 1.5 0.332119 0.332119 3538 55135.0 1.5 0.472640 0.472640 3540 55137.0 1.5 0.225573 0.225573 3542 55139.0 1.5 0.495128 0.495128 3544 55141.0 1.5 0.545278 0.545278 production_benefit_mana irri_cost_irri irri_cost_mana fer_cost_fer \ 1 0.083442 0.0 -0.012904 0.003784 3 0.539896 0.0 -0.678617 0.009765 5 0.455200 0.0 -0.284413 0.030493 7 0.786507 0.0 -0.539443 0.014210 9 0.267920 0.0 -0.181714 0.018969 ... ... ... ... ... 3536 0.332119 0.0 0.000000 0.000000 3538 0.472640 0.0 0.000000 0.000000 3540 0.225573 0.0 0.000000 0.000000 3542 0.495128 0.0 0.000000 0.000000 3544 0.545278 0.0 0.000000 0.000000 fer_cost_mana net_benefits_irri net_benefits_fer net_benefits_mana \ 1 0.004381 0.010457 0.073250 0.091965 3 0.031092 0.000000 0.499017 1.187421 5 0.039589 0.000000 0.400014 0.700024 7 0.032055 0.000000 0.735909 1.293895 9 0.030310 0.000000 0.236952 0.419324 ... ... ... ... ... 3536 0.000000 0.332119 0.332119 0.332119 3538 0.000000 0.472640 0.472640 0.472640 3540 0.000000 0.225573 0.225573 0.225573 3542 0.000000 0.495128 0.495128 0.495128 3544 0.000000 0.545278 0.545278 0.545278 state1 production_obs 1 Kansas 2.671874e+06 3 Kansas 2.351526e+07 5 Kansas 2.113825e+07 7 Kansas 4.318602e+07 9 Kansas 1.521876e+07 ... ... ... 3536 Wisconsin 2.278520e+07 3538 Wisconsin 2.831258e+07 3540 Wisconsin 1.499843e+07 3542 Wisconsin 4.461606e+07 3544 Wisconsin 1.845907e+07 [1773 rows x 14 columns] geoid scenario production_benefit_irri production_benefit_lnfer \ 0 20033.0 3.0 0.014743 0.328336 2 20119.0 3.0 0.000000 2.401122 4 20097.0 3.0 0.000000 2.153357 6 20047.0 3.0 0.000000 4.141156 8 20057.0 3.0 0.000000 1.413126 ... ... ... ... ... 3537 55133.0 3.0 1.052719 1.052719 3539 55135.0 3.0 1.892164 1.892164 3541 55137.0 3.0 0.930535 0.930535 3543 55139.0 3.0 2.019695 2.019695 3545 55141.0 3.0 2.054009 2.054009 production_benefit_mana irri_cost_irri irri_cost_mana fer_cost_fer \ 0 0.488097 0.0 -0.012904 0.082583 2 3.298127 0.0 -0.678617 0.108730 4 2.955220 0.0 -0.284413 0.544241 6 5.561677 0.0 -0.539443 0.265606 8 1.877468 0.0 -0.181714 0.318005 ... ... ... ... ... 3537 1.052719 0.0 0.000000 0.000000 3539 1.892164 0.0 0.000000 0.000000 3541 0.930535 0.0 0.000000 0.000000 3543 2.019695 0.0 0.000000 0.000000 3545 2.054009 0.0 0.000000 0.000000 fer_cost_mana net_benefits_irri net_benefits_fer net_benefits_mana \ 0 0.086940 0.014743 0.245753 0.414061 2 0.309950 0.000000 2.292392 3.666795 4 0.619872 0.000000 1.609116 2.619761 6 0.334922 0.000000 3.875550 5.766198 8 0.416482 0.000000 1.095121 1.642699 ... ... ... ... ... 3537 0.000000 1.052719 1.052719 1.052719 3539 0.000000 1.892164 1.892164 1.892164 3541 0.000000 0.930535 0.930535 0.930535 3543 0.000000 2.019695 2.019695 2.019695 3545 0.000000 2.054009 2.054009 2.054009 state1 production_obs 0 Kansas 2.671874e+06 2 Kansas 2.351526e+07 4 Kansas 2.113825e+07 6 Kansas 4.318602e+07 8 Kansas 1.521876e+07 ... ... ... 3537 Wisconsin 2.278520e+07 3539 Wisconsin 2.831258e+07 3541 Wisconsin 1.499843e+07 3543 Wisconsin 4.461606e+07 3545 Wisconsin 1.845907e+07 [1773 rows x 14 columns]
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_12641/444996450.py:5: 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_12641/444996450.py:9: 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)
positive_irri=data15[data15['net_benefits_irri']>=0]
print(np.sum(positive_irri['net_benefits_irri']))
positive_fer=data15[data15['net_benefits_fer']>=0]
print(np.sum(positive_fer['net_benefits_fer']))
positive_mana=data15[data15['net_benefits_mana']>=0]
print(np.sum(positive_mana['net_benefits_mana']))
positive_irri=data3[data3['net_benefits_irri']>=0]
print(np.sum(positive_irri['net_benefits_irri']))
positive_fer=data3[data3['net_benefits_fer']>=0]
print(np.sum(positive_fer['net_benefits_fer']))
positive_mana=data3[data3['net_benefits_mana']>=0]
print(np.sum(positive_mana['net_benefits_mana']))
744.6502473856877 1161.544126466117 1348.7481445275912 2014.089530570012 3548.03850827284 4198.88257263227
print(np.min(data15['net_benefits_irri']),np.max(data15['net_benefits_irri']),np.mean(data15['net_benefits_irri']))
print(np.percentile(data15['net_benefits_irri'], 5),np.percentile(data15['net_benefits_irri'], 95))
p95 = np.percentile(data15['net_benefits_irri'], 95)
data15.loc[(data15['net_benefits_irri'] > 4), 'net_benefits_irri'] = 4
data15.loc[(data15['net_benefits_irri'] < -4), 'net_benefits_irri'] = -4
data15['geoid'] = data15['geoid'].astype(float)
-41.188181860471495 12.218529000305756 -0.49996200832086624 -4.6352329302224184 2.0669398510697996
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_12641/1154970319.py:8: 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_benefits_irri', 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.28, 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.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_12641/1608784225.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_12641/1608784225.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)
print(np.min(data3['net_benefits_irri']),np.max(data3['net_benefits_irri']),np.mean(data3['net_benefits_irri']))
print(np.percentile(data3['net_benefits_irri'], 5),np.percentile(data3['net_benefits_irri'], 95))
p95 = np.percentile(data3['net_benefits_irri'], 95)
data3.loc[(data3['net_benefits_irri'] > 4), 'net_benefits_irri'] = 4
data3.loc[(data3['net_benefits_irri'] < -4), 'net_benefits_irri'] = -4
-45.629399914668184 38.44483577227979 -0.267450053090889 -6.656503929917799 6.486625619985444
# 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_benefits_irri', 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.28, 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.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_12641/2200681309.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_12641/2200681309.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)
print(np.min(data15['net_benefits_fer']),np.max(data15['net_benefits_fer']),np.mean(data15['net_benefits_fer']))
print(np.percentile(data15['net_benefits_fer'], 5),np.percentile(data15['net_benefits_fer'], 95))
p95 = np.percentile(data15['net_benefits_fer'], 95)
data15.loc[(data15['net_benefits_fer'] > 4), 'net_benefits_fer'] = 4
data15.loc[(data15['net_benefits_fer'] < -4), 'net_benefits_fer'] = -4
-1.3504665456979823 12.218529000305756 0.6496004120725812 0.00042329653570022006 2.687403270725774
# 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_benefits_fer', 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.28, 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.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_12641/1693412141.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_12641/1693412141.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)
print(np.min(data3['net_benefits_fer']),np.max(data3['net_benefits_fer']),np.mean(data3['net_benefits_fer']))
print(np.percentile(data3['net_benefits_fer'], 5),np.percentile(data3['net_benefits_fer'], 95))
p95 = np.percentile(data3['net_benefits_fer'], 95)
data3.loc[(data3['net_benefits_fer'] > 4), 'net_benefits_fer'] = 4
data3.loc[(data3['net_benefits_fer'] < -4), 'net_benefits_fer'] = -4
-33.11572402542053 38.44483577227979 1.7962149161376146 -0.34585911292488886 8.01398147142257
# 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_benefits_fer', 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.28, 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.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_12641/4283302815.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_12641/4283302815.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)
print(np.min(data15['net_benefits_mana']),np.max(data15['net_benefits_mana']),np.mean(data15['net_benefits_mana']))
print(np.percentile(data15['net_benefits_mana'], 5),np.percentile(data15['net_benefits_mana'], 95))
p95 = np.percentile(data15['net_benefits_mana'], 95)
data15.loc[(data15['net_benefits_mana'] > 4), 'net_benefits_mana'] = 4
data15.loc[(data15['net_benefits_mana'] < -4), 'net_benefits_mana'] = -4
-4.56889408080421 12.44529587799241 0.7393851903673297 0.0010872239068504203 2.9651256144982594
# 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_benefits_mana', 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.28, 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.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_12641/1827205446.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_12641/1827205446.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_benefits_mana', 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.12, 0.15, '(c) Soybean under\n1.5°C warming', transform=plt.gcf().transFigure,
fontsize=24, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_mana_15_.svg', format='svg',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_12641/3758812989.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_12641/3758812989.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)
print(np.min(data3['net_benefits_mana']),np.max(data3['net_benefits_mana']),np.mean(data3['net_benefits_mana']))
print(np.percentile(data3['net_benefits_mana'], 5),np.percentile(data3['net_benefits_mana'], 95))
p95 = np.percentile(data3['net_benefits_mana'], 95)
data3.loc[(data3['net_benefits_mana'] > 4), 'net_benefits_mana'] = 4
data3.loc[(data3['net_benefits_mana'] < -4), 'net_benefits_mana'] = -4
-25.24058448863475 38.44483577227979 2.265346835341488 -0.130907174252199 8.986324902196262
# 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_benefits_mana', 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.28, 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.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_12641/2530406630.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_12641/2530406630.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_benefits_mana', 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.12, 0.15, '(d) Soybean under\n3°C warming', transform=plt.gcf().transFigure,
fontsize=24, fontweight='bold', fontfamily='Arial', ha='left', va='bottom')
# Save the entire figure as a JPG with DPI 300
plt.savefig('soybean_profit_mana_3_.svg', format='svg',
dpi=300, bbox_inches='tight', pad_inches=0.1, transparent=True)
# Show the plot
plt.show()
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_12641/3226248038.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_12641/3226248038.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)