import numpy as np
import matplotlib.pyplot as plt
import netCDF4 as nc
import pandas as pd
import os
import csv
from glob import glob
import xarray as xr
import h5py
from scipy.interpolate import griddata
from shapely.geometry import Point
import geopandas as gpd
import pandas as pd
import numpy as np
import xarray as xr
import geopandas as gpd
import os
import shapely as shp
from matplotlib.font_manager import FontProperties
from matplotlib.patches import Patch
from matplotlib.legend_handler import HandlerPatch
os.chdir('/Users/chenchenren/postdoc/paper/2N and water-US/Figure 7 Cost and Benefit/')
/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 (
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Figure 5 By state/region_stata.csv"
data = pd.read_csv(excel_file)
print(data)
# Provide the path to the downloaded file
gazetteer_file_path = '/Users/chenchenren/postdoc/paper/2N and water-US/Figure 5 By state/2023_Gaz_counties_national.txt'
# Read the file into a DataFrame, specifying the delimiter (tab-separated)
gazetteer_data = pd.read_csv(gazetteer_file_path, sep='\t', dtype={'GEOID': float})
gazetteer_data = gazetteer_data[['USPS', 'GEOID']]
print(gazetteer_data)
gazetteer_data = gazetteer_data.merge(data, on='USPS', how='left')
print(gazetteer_data)
excel_file = "cost_benefit_state_maize.csv"
data1 = pd.read_csv(excel_file)
data1['STATE1']=data1['state1']
print(data1)
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Bootstrap/cost_benefit_state_bootstrap_maize.csv"
data2 = pd.read_csv(excel_file)
data2['STATE1']=data2['state1']
print(data2)
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Bootstrap/cost_benefit_country_bootstrap_maize.csv"
data3 = pd.read_csv(excel_file)
print(data3)
Region USPS STATE1 stateansi 0 E SOUTH CENTRAL AL Alabama 1 1 SOUTH CENTRAL AR Arkansas 5 2 SOUTHWEST AZ Arizona 4 3 MOUNTAIN NM New Mexico 35 4 SOUTHWEST CA California 6 5 MOUNTAIN CO Colorado 8 6 NORTHEAST CT Connecticut 9 7 NORTHEAST DE Delaware 10 8 SOUTHEAST FL Florida 12 9 SOUTHEAST GA Georgia 13 10 NORTH CENTRAL IA Iowa 19 11 NORTHWEST ID Idaho 16 12 NORTH CENTRAL IL Illinois 17 13 NORTH CENTRAL IN Indiana 18 14 NORTHERN PLAINS KS Kansas 20 15 E SOUTH CENTRAL KY Kentucky 21 16 E SOUTH CENTRAL LA Louisiana 22 17 NORTHEAST MA Massachusetts 25 18 NORTHEAST MD Maryland 24 19 NORTHEAST ME Maine 23 20 NORTH CENTRAL MI Michigan 26 21 NORTH CENTRAL MN Minnesota 27 22 NORTH CENTRAL MO Missouri 29 23 E SOUTH CENTRAL MS Mississippi 28 24 MOUNTAIN MT Montana 30 25 SOUTHEAST NC North Carolina 37 26 NORTHERN PLAINS ND North Dakota 38 27 NORTHERN PLAINS NE Nebraska 31 28 NORTHEAST NH New Hampshire 33 29 NORTHEAST NJ New Jersey 34 30 SOUTHWEST NV Nevada 32 31 NORTHEAST NY New York 36 32 NORTH CENTRAL OH Ohio 39 33 SOUTH CENTRAL OK Oklahoma 40 34 NORTHWEST OR Oregon 41 35 NORTHEAST PA Pennsylvania 42 36 NORTHEAST RI Rhode Island 44 37 SOUTHEAST SC South Carolina 45 38 NORTHERN PLAINS SD South Dakota 46 39 E SOUTH CENTRAL TN Tennessee 47 40 SOUTH CENTRAL TX Texas 48 41 SOUTHWEST UT Utah 49 42 SOUTHEAST VA Virginia 51 43 NORTHEAST VT Vermont 50 44 NORTHWEST WA Washington 53 45 NORTH CENTRAL WI Wisconsin 55 46 NORTHEAST WV West Virginia 54 47 MOUNTAIN WY Wyoming 56 USPS GEOID 0 AL 1001.0 1 AL 1003.0 2 AL 1005.0 3 AL 1007.0 4 AL 1009.0 ... ... ... 3217 PR 72145.0 3218 PR 72147.0 3219 PR 72149.0 3220 PR 72151.0 3221 PR 72153.0 [3222 rows x 2 columns] USPS GEOID Region STATE1 stateansi 0 AL 1001.0 E SOUTH CENTRAL Alabama 1.0 1 AL 1003.0 E SOUTH CENTRAL Alabama 1.0 2 AL 1005.0 E SOUTH CENTRAL Alabama 1.0 3 AL 1007.0 E SOUTH CENTRAL Alabama 1.0 4 AL 1009.0 E SOUTH CENTRAL Alabama 1.0 ... ... ... ... ... ... 3217 PR 72145.0 NaN NaN NaN 3218 PR 72147.0 NaN NaN NaN 3219 PR 72149.0 NaN NaN NaN 3220 PR 72151.0 NaN NaN NaN 3221 PR 72153.0 NaN NaN NaN [3222 rows x 5 columns] scenario state1 production_benefit_irri_t irri_cost_irri_t \ 0 1.5 Alabama 4.879672 47.436064 1 1.5 Arizona 0.014975 -0.973851 2 1.5 Arkansas 22.125504 11.637944 3 1.5 California 0.090976 -12.704181 4 1.5 Colorado 5.372672 -1.004585 .. ... ... ... ... 77 3.0 Virginia 11.224737 14.901896 78 3.0 Washington 0.978249 0.033884 79 3.0 West Virginia -0.627541 0.872365 80 3.0 Wisconsin 76.792310 159.568379 81 3.0 Wyoming -1.468449 0.000000 net_benefits_irri_t production_benefit_lnfer_t fer_cost_fer_t \ 0 -42.556392 4.757390 5.465057 1 0.988826 0.179789 0.140677 2 10.487560 23.765683 28.125573 3 12.795157 0.769129 0.565626 4 6.377257 8.235001 8.776532 .. ... ... ... 77 -3.677159 10.143418 12.988527 78 0.944366 0.977969 0.121375 79 -1.499905 0.413538 0.267461 80 -82.776069 94.286536 34.105508 81 -1.468449 1.021636 0.364928 net_benefits_lnfer_t production_benefit_mana_t irri_cost_mana_t \ 0 -0.707667 4.954810 51.258956 1 0.039111 0.182371 -1.269251 2 -4.359890 25.304751 -0.533677 3 0.203503 0.803141 -16.069203 4 -0.541531 8.474843 -10.340117 .. ... ... ... 77 -2.845109 10.882128 4.512142 78 0.856593 0.984076 -0.563476 79 0.146077 0.428265 -0.002579 80 60.181028 116.473592 35.226427 81 0.656708 1.027802 0.000000 fer_cost_mana_t net_benefits_mana_t STATE1 0 0.889806 -47.193952 Alabama 1 7.043417 -5.591796 Arizona 2 29.145543 -3.307115 Arkansas 3 38.876171 -22.003827 California 4 71.388871 -52.573911 Colorado .. ... ... ... 77 8.905870 -2.535884 Virginia 78 3.683876 -2.136324 Washington 79 0.267547 0.163296 West Virginia 80 28.271662 52.975504 Wisconsin 81 0.364928 0.662874 Wyoming [82 rows x 13 columns] state1 scenario net_benefit_mana_p5 net_benefit_mana_p95 \ 0 Alabama 1.5 -45.787689 -34.966788 1 Alabama 3.0 -56.555396 -44.404967 2 Arizona 1.5 -7.654541 -3.755376 3 Arizona 3.0 -17.436745 -6.955497 4 Arkansas 1.5 -5.794230 -1.171783 .. ... ... ... ... 76 West Virginia 3.0 0.032219 0.104679 77 Wisconsin 1.5 -47.185144 5.942195 78 Wisconsin 3.0 -25.771223 23.140534 79 Wyoming 1.5 -0.004829 -0.002292 80 Wyoming 3.0 -0.063699 -0.006163 net_benefit_mana_med STATE1 0 -42.879356 Alabama 1 -52.268728 Alabama 2 -5.555905 Arizona 3 -11.561146 Arizona 4 -3.324119 Arkansas .. ... ... 76 0.063504 West Virginia 77 -22.340036 Wisconsin 78 -2.786209 Wisconsin 79 -0.003212 Wyoming 80 -0.027472 Wyoming [81 rows x 6 columns] scenario net_benefit_mana_p5 net_benefit_mana_p95 net_benefit_mana_med 0 1.5 -600.887351 -437.889286 -518.737020 1 3.0 -488.393805 -59.436809 -259.928662
err=data2[['STATE1','net_benefit_mana_p95', 'net_benefit_mana_p5', 'net_benefit_mana_med','scenario']]
err['p95']=np.abs(err['net_benefit_mana_p95']-err['net_benefit_mana_med'])
err['p5']=np.abs(err['net_benefit_mana_p5']-err['net_benefit_mana_med'])
err=err[['STATE1','p95', 'p5','scenario']]
print(err)
err3=err[err['scenario']==3]
err15=err[err['scenario']==1.5]
err15.rename(columns={'p95': 'p95_15'}, inplace=True)
err15.rename(columns={'p5': 'p5_15'}, inplace=True)
err15=err15[['STATE1','p95_15', 'p5_15']]
err3.rename(columns={'p95': 'p95_3'}, inplace=True)
err3.rename(columns={'p5': 'p5_3'}, inplace=True)
err3=err3[['STATE1','p95_3', 'p5_3']]
err = err15.merge(err3, on='STATE1', how='left')
print(err)
err_country=data3[['net_benefit_mana_p95', 'net_benefit_mana_p5', 'net_benefit_mana_med','scenario']]
err_country['STATE1'] = 'U.S.'
err_country['p95']=np.abs(err_country['net_benefit_mana_p95']-err_country['net_benefit_mana_med'])
err_country['p5']=np.abs(err_country['net_benefit_mana_p5']-err_country['net_benefit_mana_med'])
err_country=err_country[['STATE1','p95', 'p5','scenario']]
print(err_country)
err_country3=err_country[err_country['scenario']==3]
err_country15=err_country[err_country['scenario']==1.5]
err_country15.rename(columns={'p95': 'p95_15'}, inplace=True)
err_country15.rename(columns={'p5': 'p5_15'}, inplace=True)
err_country15=err_country15[['STATE1','p95_15', 'p5_15']]
err_country3.rename(columns={'p95': 'p95_3'}, inplace=True)
err_country3.rename(columns={'p5': 'p5_3'}, inplace=True)
err_country3=err_country3[['STATE1','p95_3', 'p5_3']]
err_country = err_country15.merge(err_country3, on='STATE1', how='left')
print(err_country)
STATE1 p95 p5 scenario 0 Alabama 7.912567 2.908333 1.5 1 Alabama 7.863761 4.286667 3.0 2 Arizona 1.800529 2.098636 1.5 3 Arizona 4.605649 5.875599 3.0 4 Arkansas 2.152336 2.470111 1.5 .. ... ... ... ... 76 West Virginia 0.041175 0.031285 3.0 77 Wisconsin 28.282231 24.845108 1.5 78 Wisconsin 25.926743 22.985013 3.0 79 Wyoming 0.000921 0.001616 1.5 80 Wyoming 0.021309 0.036228 3.0 [81 rows x 4 columns] STATE1 p95_15 p5_15 p95_3 p5_3 0 Alabama 7.912567 2.908333 7.863761 4.286667 1 Arizona 1.800529 2.098636 4.605649 5.875599 2 Arkansas 2.152336 2.470111 4.458191 5.710817 3 California 5.895876 7.209721 8.026046 8.882741 4 Colorado 9.153045 10.577893 18.012193 20.412317 5 Delaware 4.068142 0.365992 0.883774 0.894908 6 Florida 0.231984 0.162147 0.348088 0.381723 7 Georgia 0.240332 0.221807 2.168093 2.491529 8 Idaho 1.503089 1.758477 0.355784 0.380268 9 Illinois 13.793259 13.701582 41.422874 34.696402 10 Indiana 8.253698 34.510639 20.438115 27.382602 11 Iowa 10.178128 10.097300 45.696375 37.677518 12 Kansas 23.584463 27.431522 41.196857 44.925498 13 Kentucky 0.002481 0.036193 55.121798 36.611809 14 Louisiana 1.089717 13.882161 14.364151 21.078965 15 Maryland 3.298407 1.948239 9.014634 2.217569 16 Michigan 14.033695 11.885682 10.557099 4.908333 17 Minnesota 31.981381 15.944096 41.030005 19.469300 18 Mississippi 1.798541 4.454730 10.491290 14.092566 19 Missouri 21.952346 15.488390 21.065334 19.988511 20 Montana 0.004416 0.005316 0.003934 0.001244 21 Nebraska 0.191944 0.199711 18.409201 18.121035 22 New Jersey 0.023940 0.043192 0.242539 0.400101 23 New Mexico 2.301700 2.029016 3.458806 2.562475 24 New York 11.019552 7.915989 9.015740 5.653909 25 North Carolina 12.642997 3.418873 13.064389 5.525733 26 North Dakota 0.260136 0.128036 3.546064 3.243519 27 Ohio 42.844561 21.592443 48.235033 26.390141 28 Oklahoma 0.929226 0.946098 5.835480 6.171107 29 Oregon 0.006436 0.006283 0.000000 0.000000 30 Pennsylvania 8.165070 2.004906 27.543269 9.863777 31 South Carolina 0.000506 0.000563 2.736863 2.400129 32 South Dakota 1.278880 1.252886 2.566919 1.611069 33 Tennessee 11.596545 4.332103 12.047678 7.128796 34 Texas 10.530596 12.746126 66.268111 117.948700 35 Utah 0.918306 1.218848 0.098841 0.116142 36 Virginia 0.432152 0.469851 0.982170 1.177419 37 Washington 1.928851 1.997461 0.971904 0.718903 38 Wisconsin 28.282231 24.845108 25.926743 22.985013 39 Wyoming 0.000921 0.001616 0.021309 0.036228 STATE1 p95 p5 scenario 0 U.S. 80.847734 82.150331 1.5 1 U.S. 200.491853 228.465142 3.0 STATE1 p95_15 p5_15 p95_3 p5_3 0 U.S. 80.847734 82.150331 200.491853 228.465142
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:8: 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 err15.rename(columns={'p95': 'p95_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.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 err15.rename(columns={'p5': 'p5_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:11: 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 err3.rename(columns={'p95': 'p95_3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:12: 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 err3.rename(columns={'p5': 'p5_3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.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 err_country['STATE1'] = 'U.S.' /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:20: 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 err_country['p95']=np.abs(err_country['net_benefit_mana_p95']-err_country['net_benefit_mana_med']) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:21: 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 err_country['p5']=np.abs(err_country['net_benefit_mana_p5']-err_country['net_benefit_mana_med']) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:26: 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 err_country15.rename(columns={'p95': 'p95_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:27: 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 err_country15.rename(columns={'p5': 'p5_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:29: 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 err_country3.rename(columns={'p95': 'p95_3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_18125/3233285433.py:30: 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 err_country3.rename(columns={'p5': 'p5_3'}, inplace=True)
country15=data1[data1['scenario']==1.5]
country3=data1[data1['scenario']==3]
print(country3)
sum15 = country15['net_benefits_mana_t'].sum()
sum3 = country3['net_benefits_mana_t'].sum()
print(sum15, sum3)
scenario state1 production_benefit_irri_t irri_cost_irri_t \ 41 3.0 Alabama 11.987205 89.524599 42 3.0 Arizona 0.000000 -1.217313 43 3.0 Arkansas 31.237887 13.292704 44 3.0 California 0.283529 -12.473320 45 3.0 Colorado 13.338733 -1.568040 46 3.0 Delaware 4.746894 12.970800 47 3.0 Florida 0.477545 0.439836 48 3.0 Georgia 5.137510 2.279867 49 3.0 Idaho 1.555973 0.006098 50 3.0 Illinois 365.936460 272.618155 51 3.0 Indiana 162.468232 220.869914 52 3.0 Iowa 565.093718 358.051527 53 3.0 Kansas 122.242473 66.967354 54 3.0 Kentucky 20.727542 169.594898 55 3.0 Louisiana 149.049934 58.941670 56 3.0 Maryland 13.277485 86.202764 57 3.0 Michigan 29.993410 86.886954 58 3.0 Minnesota 157.852603 1215.656678 59 3.0 Mississippi 127.784233 46.382047 60 3.0 Missouri 168.907527 394.298594 61 3.0 Montana 0.082104 0.097048 62 3.0 Nebraska 280.504322 102.159006 63 3.0 New Jersey -4.453579 13.962663 64 3.0 New Mexico 0.096804 -1.129107 65 3.0 New York 16.511596 91.547848 66 3.0 North Carolina 21.612220 198.408622 67 3.0 North Dakota 38.270196 11.267374 68 3.0 Ohio 45.427711 178.781521 69 3.0 Oklahoma 42.822451 5.390849 70 3.0 Oregon 0.539861 0.000000 71 3.0 Pennsylvania 12.386887 56.098821 72 3.0 South Carolina 3.210843 6.490212 73 3.0 South Dakota 38.274715 7.529255 74 3.0 Tennessee 34.001762 215.169373 75 3.0 Texas 450.534188 47.747903 76 3.0 Utah 0.317105 -0.016122 77 3.0 Virginia 11.224737 14.901896 78 3.0 Washington 0.978249 0.033884 79 3.0 West Virginia -0.627541 0.872365 80 3.0 Wisconsin 76.792310 159.568379 81 3.0 Wyoming -1.468449 0.000000 net_benefits_irri_t production_benefit_lnfer_t fer_cost_fer_t \ 41 -77.537395 13.201945 20.122497 42 1.217313 0.364102 0.338186 43 17.945184 43.928269 55.279710 44 12.756849 1.012136 0.473076 45 14.906772 25.164689 27.386566 46 -8.223906 5.816281 1.850556 47 0.037709 0.358409 0.679500 48 2.857643 5.141118 6.129736 49 1.549875 1.557106 0.137419 50 93.318305 348.135397 122.342603 51 -58.401682 153.798790 64.593082 52 207.042191 402.618310 148.513877 53 55.275119 161.489236 159.821938 54 -148.867356 20.631374 7.741614 55 90.108264 78.690828 92.238367 56 -72.925279 12.793271 12.411822 57 -56.893544 31.151857 27.845365 58 -1057.804076 216.625405 139.162294 59 81.402185 99.679203 128.539857 60 -225.391068 145.099652 129.552865 61 -0.014943 0.611255 0.022667 62 178.345316 224.595450 93.559086 63 -18.416242 4.090574 3.980864 64 1.225911 0.652157 0.554870 65 -75.036252 17.183942 11.182360 66 -176.796402 21.214368 29.772004 67 27.002822 41.526327 3.489512 68 -133.353810 60.147713 23.137406 69 37.431602 32.610842 31.106373 70 0.539861 0.539861 0.000000 71 -43.711934 12.271019 3.702518 72 -3.279369 3.140815 6.042172 73 30.745461 41.261961 15.752517 74 -181.167611 34.855214 42.106786 75 402.786285 245.870110 303.035986 76 0.333227 0.323881 0.006775 77 -3.677159 10.143418 12.988527 78 0.944366 0.977969 0.121375 79 -1.499905 0.413538 0.267461 80 -82.776069 94.286536 34.105508 81 -1.468449 1.021636 0.364928 net_benefits_lnfer_t production_benefit_mana_t irri_cost_mana_t \ 41 -6.920552 14.480613 59.218811 42 0.025916 0.375240 -1.586563 43 -11.351441 49.105241 -0.696156 44 0.539059 1.041692 -15.764170 45 -2.221877 26.488082 -15.675949 46 3.965725 6.095637 -4.628770 47 -0.321091 0.385369 -0.149093 48 -0.988618 5.344934 -1.321938 49 1.419687 1.557818 -0.076992 50 225.792794 393.137966 34.501995 51 89.205708 186.161800 111.705708 52 254.104433 457.061992 32.525324 53 1.667298 174.829830 -79.980127 54 12.889759 20.813072 128.355430 55 -13.547539 100.875962 7.026770 56 0.381449 13.510087 45.340710 57 3.306492 32.265552 68.097207 58 77.463111 226.925759 113.196963 59 -28.860655 128.131829 21.205941 60 15.546787 165.321537 34.578388 61 0.588588 0.611375 0.000000 62 131.036364 236.929018 0.001990 63 0.109710 4.527540 1.760163 64 0.097287 0.682443 -3.255271 65 6.001583 18.065620 73.448879 66 -8.557636 22.433860 122.184507 67 38.036815 42.122043 0.000000 68 37.010307 61.270338 146.373317 69 1.504470 41.118147 0.507881 70 0.539861 0.539861 0.000000 71 8.568501 12.477686 131.679869 72 -2.901357 3.250831 6.484161 73 25.509444 41.796033 -0.528652 74 -7.251572 37.887343 72.756698 75 -57.165875 315.099660 0.000000 76 0.317106 0.323931 -0.030362 77 -2.845109 10.882128 4.512142 78 0.856593 0.984076 -0.563476 79 0.146077 0.428265 -0.002579 80 60.181028 116.473592 35.226427 81 0.656708 1.027802 0.000000 fer_cost_mana_t net_benefits_mana_t STATE1 41 11.910019 -56.648217 Alabama 42 15.288586 -13.326784 Arizona 43 58.135727 -8.334330 Arkansas 44 37.034896 -20.229034 California 45 147.877620 -105.713589 Colorado 46 3.021913 7.702494 Delaware 47 0.867387 -0.332925 Florida 48 10.007837 -3.340965 Georgia 49 1.936115 -0.301305 Idaho 50 112.539776 246.096195 Illinois 51 37.222630 37.233461 Indiana 52 135.913214 288.623455 Iowa 53 380.747251 -125.937294 Kansas 54 3.187818 -110.730176 Kentucky 55 79.526109 14.323084 Louisiana 56 7.917684 -39.748307 Maryland 57 6.939997 -42.771651 Michigan 58 131.606694 -17.877897 Minnesota 59 79.783319 27.142570 Mississippi 60 120.606928 10.136221 Missouri 61 0.022667 0.588709 Montana 62 93.555181 143.371848 Nebraska 63 3.826720 -1.059343 New Jersey 64 12.676285 -8.738571 New Mexico 65 3.658436 -59.041695 New York 66 10.587787 -110.338434 North Carolina 67 3.489512 38.632532 North Dakota 68 6.672180 -91.775159 Ohio 69 27.866040 12.744226 Oklahoma 70 0.000000 0.539861 Oregon 71 -4.300162 -114.902020 Pennsylvania 72 1.474667 -4.707998 South Carolina 73 17.451358 24.873327 South Dakota 74 30.438064 -65.307419 Tennessee 75 303.035986 12.063674 Texas 76 0.594931 -0.240638 Utah 77 8.905870 -2.535884 Virginia 78 3.683876 -2.136324 Washington 79 0.267547 0.163296 West Virginia 80 28.271662 52.975504 Wisconsin 81 0.364928 0.662874 Wyoming -21.977724896734088 -88.20262858000046
print(country15['net_benefits_irri_t'].sum())
print(country3['net_benefits_irri_t'].sum())
print(country15['net_benefits_lnfer_t'].sum())
print(country3['net_benefits_lnfer_t'].sum())
-932.4413983692075 -1169.4701910642018 856.947471547429 854.5353372106669
df_sum = pd.DataFrame({
'sum3': [sum3],
'sum15': [sum15]
})
df_sum['STATE1'] = 'U.S.'
print(df_sum)
df_sum=df_sum.merge(err_country, left_on='STATE1', right_on='STATE1')
print(df_sum)
# Define columns for plotting
columns_to_plot = ['sum15', 'p5_15', 'p95_15',
'sum3', 'p5_3', 'p95_3']
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = [ 'cadetblue','lightsalmon']
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the x-axis
index = np.arange(len(df_sum['STATE1']))
# Expand the length of the y-axis
plt.figure(figsize=(5, 1))
# Create a bar for each attribute
for i in range(num_attributes // 3):
plt.barh(index + i * bar_width, df_sum[columns_to_plot[i * 3]],
bar_width,label=f'{columns_to_plot[i * 3]}', color=colors[i], edgecolor='black', # Add edge color
linewidth=0.7, capsize=3, capstyle='round') # Set capstyle to 'round'
main_data = df_sum[columns_to_plot[i* 3]]
error_lower = (df_sum[columns_to_plot[i* 3 + 1]]).values
error_upper = (df_sum[columns_to_plot[i* 3 + 2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index + (i) * bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
# Set the y-axis labels to be the state names
plt.yticks([0.2], df_sum['STATE1'],fontsize=24, fontfamily='Arial')
# Set the x-ticks
x_ticks = np.arange(-360, 190, 180)
plt.xticks(x_ticks)
# Add a dashed line at x=0
ax = plt.gca()
# Set tick label font sizes
ax.tick_params(axis='both', labelsize=24)
ax.set_xticklabels(ax.get_xticklabels(), fontfamily='Arial')
ax.set_yticklabels(ax.get_yticklabels(), fontfamily='Arial')
# Save the entire figure as a JPG with DPI 300
plt.savefig('national_maize_net_benefit.svg', format='svg', dpi=300, bbox_inches='tight', pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
sum3 sum15 STATE1 0 -88.202629 -21.977725 U.S. sum3 sum15 STATE1 p95_15 p5_15 p95_3 p5_3 0 -88.202629 -21.977725 U.S. 80.847734 82.150331 200.491853 228.465142
county15=data1[data1['scenario']==1.5]
county3=data1[data1['scenario']==3]
county15 = county15[['STATE1','net_benefits_mana_t']]
county3 = county3[['STATE1','net_benefits_mana_t']]
county15.rename(columns={'net_benefits_mana_t': 'net_benefits_mana_t15'}, inplace=True)
county3.rename(columns={'net_benefits_mana_t': 'net_benefits_mana_t3'}, inplace=True)
county=county15.merge(county3, left_on='STATE1', right_on='STATE1')
county=county.merge(err, left_on='STATE1', right_on='STATE1')
print(county)
STATE1 net_benefits_mana_t15 net_benefits_mana_t3 p95_15 \ 0 Alabama -47.193952 -56.648217 7.912567 1 Arizona -5.591796 -13.326784 1.800529 2 Arkansas -3.307115 -8.334330 2.152336 3 California -22.003827 -20.229034 5.895876 4 Colorado -52.573911 -105.713589 9.153045 5 Delaware 8.426922 7.702494 4.068142 6 Florida 0.208563 -0.332925 0.231984 7 Georgia 5.601612 -3.340965 0.240332 8 Idaho -8.515112 -0.301305 1.503089 9 Illinois 124.362356 246.096195 13.793259 10 Indiana 40.755902 37.233461 8.253698 11 Iowa 107.086981 288.623455 10.178128 12 Kansas -71.612300 -125.937294 23.584463 13 Kentucky 42.091844 -110.730176 0.002481 14 Louisiana -2.270281 14.323084 1.089717 15 Maryland -22.732404 -39.748307 3.298407 16 Michigan -123.355881 -42.771651 14.033695 17 Minnesota -33.091896 -17.877897 31.981381 18 Mississippi -1.529858 27.142570 1.798541 19 Missouri -61.685459 10.136221 21.952346 20 Montana 0.406804 0.588709 0.004416 21 Nebraska 200.608088 143.371848 0.191944 22 New Jersey -1.837167 -1.059343 0.023940 23 New Mexico -6.335492 -8.738571 2.301700 24 New York -52.627431 -59.041695 11.019552 25 North Carolina -71.043121 -110.338434 12.642997 26 North Dakota 34.326623 38.632532 0.260136 27 Ohio -24.170063 -91.775159 42.844561 28 Oklahoma 3.444380 12.744226 0.929226 29 Oregon 0.464787 0.539861 0.006436 30 Pennsylvania -0.514977 -114.902020 8.165070 31 South Carolina 2.997085 -4.707998 0.000506 32 South Dakota 45.750523 24.873327 1.278880 33 Tennessee -74.091893 -65.307419 11.596545 34 Texas 25.227653 12.063674 10.530596 35 Utah -3.419668 -0.240638 0.918306 36 Virginia -0.980752 -2.535884 0.432152 37 Washington -2.285785 -2.136324 1.928851 38 Wisconsin 28.018358 52.975504 28.282231 39 Wyoming 0.451793 0.662874 0.000921 p5_15 p95_3 p5_3 0 2.908333 7.863761 4.286667 1 2.098636 4.605649 5.875599 2 2.470111 4.458191 5.710817 3 7.209721 8.026046 8.882741 4 10.577893 18.012193 20.412317 5 0.365992 0.883774 0.894908 6 0.162147 0.348088 0.381723 7 0.221807 2.168093 2.491529 8 1.758477 0.355784 0.380268 9 13.701582 41.422874 34.696402 10 34.510639 20.438115 27.382602 11 10.097300 45.696375 37.677518 12 27.431522 41.196857 44.925498 13 0.036193 55.121798 36.611809 14 13.882161 14.364151 21.078965 15 1.948239 9.014634 2.217569 16 11.885682 10.557099 4.908333 17 15.944096 41.030005 19.469300 18 4.454730 10.491290 14.092566 19 15.488390 21.065334 19.988511 20 0.005316 0.003934 0.001244 21 0.199711 18.409201 18.121035 22 0.043192 0.242539 0.400101 23 2.029016 3.458806 2.562475 24 7.915989 9.015740 5.653909 25 3.418873 13.064389 5.525733 26 0.128036 3.546064 3.243519 27 21.592443 48.235033 26.390141 28 0.946098 5.835480 6.171107 29 0.006283 0.000000 0.000000 30 2.004906 27.543269 9.863777 31 0.000563 2.736863 2.400129 32 1.252886 2.566919 1.611069 33 4.332103 12.047678 7.128796 34 12.746126 66.268111 117.948700 35 1.218848 0.098841 0.116142 36 0.469851 0.982170 1.177419 37 1.997461 0.971904 0.718903 38 24.845108 25.926743 22.985013 39 0.001616 0.021309 0.036228
state_sum_10=county.nlargest(10, 'net_benefits_mana_t15')
state_sum_10 = state_sum_10.sort_values(by='net_benefits_mana_t3', ascending=False)
print(state_sum_10)
output_csv_file = "maize_figure5.csv"
state_sum_10.to_csv(output_csv_file, index=False)
# Define the columns you want to plot
columns_to_plot = ['net_benefits_mana_t15', 'p5_15', 'p95_15','net_benefits_mana_t3', 'p5_3', 'p95_3']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = [ 'cadetblue','lightsalmon']
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the x-axis
index = np.arange(len(state_sum_10['STATE1']))
# Expand the length of the y-axis
plt.figure(figsize=(5, 12))
# Create a bar for each attribute
for i in range(num_attributes // 3):
plt.barh(index + i * bar_width, state_sum_10[columns_to_plot[i * 3]],
bar_width,label=f'{columns_to_plot[i * 3]}', color=colors[i], edgecolor='black', # Add edge color
linewidth=0.7) # Set capstyle to 'round'
main_data = state_sum_10[columns_to_plot[i* 3]]
error_lower = (state_sum_10[columns_to_plot[i* 3 + 1]]).values
error_upper = (state_sum_10[columns_to_plot[i* 3 + 2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index + (i) * bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
# Set the y-axis labels to be the state names
plt.yticks(index + [0.2], state_sum_10['STATE1'])
# Set the x-axis label
plt.xlabel('Cost-competitiveness\n(Million $/yr)', fontsize=24, fontfamily='Arial')
# Set the x-ticks
x_ticks = np.arange(-140, 700, 200)
plt.xticks(x_ticks)
# Add a dashed line at x=0
ax = plt.gca()
ax.axvline(0, color='black', linestyle='--', linewidth=1)
plt.text(0.8, 0.82, '(e) Maize', transform=plt.gcf().transFigure,
fontsize=24, fontweight='bold', fontfamily='Arial', ha='right', va='bottom')
# Set tick label font sizes
ax.tick_params(axis='both', labelsize=24)
ax.set_xticklabels(ax.get_xticklabels(), fontfamily='Arial')
ax.set_yticklabels(ax.get_yticklabels(), fontfamily='Arial')
# Save the entire figure as a JPG with DPI 300
plt.savefig('state_maize_net_benefit_10.svg', format='svg', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
STATE1 net_benefits_mana_t15 net_benefits_mana_t3 p95_15 \ 11 Iowa 107.086981 288.623455 10.178128 9 Illinois 124.362356 246.096195 13.793259 21 Nebraska 200.608088 143.371848 0.191944 38 Wisconsin 28.018358 52.975504 28.282231 26 North Dakota 34.326623 38.632532 0.260136 10 Indiana 40.755902 37.233461 8.253698 32 South Dakota 45.750523 24.873327 1.278880 34 Texas 25.227653 12.063674 10.530596 5 Delaware 8.426922 7.702494 4.068142 13 Kentucky 42.091844 -110.730176 0.002481 p5_15 p95_3 p5_3 11 10.097300 45.696375 37.677518 9 13.701582 41.422874 34.696402 21 0.199711 18.409201 18.121035 38 24.845108 25.926743 22.985013 26 0.128036 3.546064 3.243519 10 34.510639 20.438115 27.382602 32 1.252886 2.566919 1.611069 34 12.746126 66.268111 117.948700 5 0.365992 0.883774 0.894908 13 0.036193 55.121798 36.611809
print(state_sum_10)
STATE1 net_benefits_mana_t15 net_benefits_mana_t3 p95_15 \ 11 Iowa 107.086981 288.623455 10.178128 9 Illinois 124.362356 246.096195 13.793259 21 Nebraska 200.608088 143.371848 0.191944 38 Wisconsin 28.018358 52.975504 28.282231 26 North Dakota 34.326623 38.632532 0.260136 10 Indiana 40.755902 37.233461 8.253698 32 South Dakota 45.750523 24.873327 1.278880 34 Texas 25.227653 12.063674 10.530596 5 Delaware 8.426922 7.702494 4.068142 13 Kentucky 42.091844 -110.730176 0.002481 p5_15 p95_3 p5_3 11 10.097300 45.696375 37.677518 9 13.701582 41.422874 34.696402 21 0.199711 18.409201 18.121035 38 24.845108 25.926743 22.985013 26 0.128036 3.546064 3.243519 10 34.510639 20.438115 27.382602 32 1.252886 2.566919 1.611069 34 12.746126 66.268111 117.948700 5 0.365992 0.883774 0.894908 13 0.036193 55.121798 36.611809
class HandlerHatch(HandlerPatch):
def __init__(self, edge_color='black', edge_width=1.0):
super().__init__()
self.edge_color = edge_color
self.edge_width = edge_width
def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans):
artists = super().create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
# Apply edge color and width only to the last patch
for i, artist in enumerate(artists):
if i == len(artists) - 1 and self.edge_color:
artist.set_edgecolor(self.edge_color)
artist.set_linewidth(self.edge_width)
return artists
state_sum_10_1 = state_sum_10.tail(6)
# Define the columns you want to plot
columns_to_plot = ['net_benefits_mana_t15', 'p5_15', 'p95_15','net_benefits_mana_t3', 'p5_3', 'p95_3']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['cadetblue','salmon']
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the x-axis
index = np.arange(len(state_sum_10_1['STATE1']))
# Expand the length of the y-axis
plt.figure(figsize=(15, 5), facecolor='none')
# Create a bar for each attribute
for i in range(num_attributes // 3):
plt.barh(index + i * bar_width, state_sum_10_1[columns_to_plot[i * 3]],
bar_width,label=f'{columns_to_plot[i * 3]}', color=colors[i], edgecolor='black', # Add edge color
linewidth=0.7) # Set capstyle to 'round'
main_data = state_sum_10_1[columns_to_plot[i* 3]]
error_lower = (state_sum_10_1[columns_to_plot[i* 3 + 1]]).values
error_upper = (state_sum_10_1[columns_to_plot[i* 3 + 2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index + (i) * bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
# Set the y-axis labels to be the state names
plt.yticks(index + bar_width * (num_attributes - 1) / 2-[0.5], state_sum_10_1['STATE1'])
# Set the x-ticks
x_ticks = np.arange(-100, 600, 200)
plt.xticks(x_ticks, fontfamily='Arial')
# Add a dashed line at x=0
ax = plt.gca()
ax.axvline(0, color='black', linestyle='--', linewidth=1)
# Add a legend
legend_labels = ['N requirement = 0', '3.0°C warming',
'1.5°C warming' ]
handles = [
Patch(facecolor='lightsalmon', label=legend_labels[0]),
Patch(facecolor='lightsalmon', label=legend_labels[1]),
Patch(facecolor='cadetblue', label=legend_labels[2])
]
handler = HandlerHatch(edge_color='black', edge_width=0.7)
font_prop = FontProperties(family='Arial', size=30)
plt.legend(handles=[handles[1], handles[2]],
labels=[legend_labels[1], legend_labels[2]],
handler_map={Patch: handler}, prop=font_prop, loc='center right')
# Set tick label font sizes
ax.tick_params(axis='both', labelsize=20)
ax.set_xticklabels(ax.get_xticklabels(), fontfamily='Arial')
ax.set_yticklabels(ax.get_yticklabels(), fontfamily='Arial')
# Save the entire figure as a JPG with DPI 300
plt.savefig('net_benefit_legend.svg', format='svg', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()