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
from matplotlib.lines import Line2D
from matplotlib.patches import Patch
from matplotlib.legend_handler import HandlerPatch
/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 6 sustain and unsustain/')
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)
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]
# Load data from an Excel file
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Figure 4 Pattern/pattern_base.xlsx"
sheet_name = "soybean"
data1 = pd.read_excel(excel_file, sheet_name=sheet_name)
data1['obs_t']=data1['irrigation1']*data1['har']/1000000 #km3
print(data1)
data1['geoid'] = data1['geoid'].astype(float)
# Rename the 'geoid' column to 'GEOID' in year_2020_data
data1.rename(columns={'geoid': 'GEOID'}, inplace=True)
obs = data1.merge(gazetteer_data, on='GEOID', how='left')
columns_to_sum = ['obs_t']
obs_t = obs.groupby('STATE1')[columns_to_sum].sum().reset_index()
print(obs_t)
geoid irrigation1 har yield production fer obs_t 0 1001 0.377088 779.625 1.825626 0.142330 0.011677 0.000294 1 1003 0.738441 6358.433 2.772287 1.762740 0.003642 0.004695 2 1005 0.028216 526.500 3.503725 0.184471 0.008287 0.000015 3 1009 0.056342 1107.736 2.541985 0.281585 0.078394 0.000062 4 1011 0.030586 870.750 2.935445 0.255604 0.128827 0.000027 ... ... ... ... ... ... ... ... 1768 55133 0.000000 7837.872 2.907065 2.278520 0.004555 0.000000 1769 55135 0.000000 9909.051 2.857244 2.831258 0.004333 0.000000 1770 55137 0.000000 5335.875 2.810866 1.499843 0.004727 0.000000 1771 55139 0.000000 14541.420 3.068204 4.461603 0.003508 0.000000 1772 55141 0.000000 6700.507 2.754878 1.845908 0.012887 0.000000 [1773 rows x 7 columns] STATE1 obs_t 0 Alabama 2.672979e-02 1 Arkansas 1.305501e+00 2 Delaware 3.923567e-02 3 Florida 2.030791e-04 4 Georgia 4.250739e-02 5 Illinois 8.982157e-02 6 Indiana 2.246766e-02 7 Iowa 1.635207e-02 8 Kansas 2.619782e-01 9 Kentucky 6.512101e-03 10 Louisiana 7.858539e-09 11 Maryland 2.575924e-02 12 Michigan 5.041988e-02 13 Minnesota 4.104377e-02 14 Mississippi 1.008715e-06 15 Missouri 1.095012e-05 16 Nebraska 3.799619e-06 17 New Jersey 7.452582e-08 18 New York 6.661501e-04 19 North Carolina 8.682545e-02 20 North Dakota 0.000000e+00 21 Ohio 6.057680e-03 22 Oklahoma 0.000000e+00 23 Pennsylvania 6.010994e-04 24 South Carolina 2.646569e-02 25 South Dakota 4.024230e-02 26 Tennessee 3.759947e-02 27 Texas 0.000000e+00 28 Virginia 1.527527e-02 29 West Virginia 6.815925e-07 30 Wisconsin 0.000000e+00
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Bootstrap/soybean_irri_ci.csv"
err = pd.read_csv(excel_file)
print(err)
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Bootstrap/soybean_irri_country_ci.csv"
err_country = pd.read_csv(excel_file)
print(err_country)
STATE1 gap_p0 gap_mana_p0 gap_p1 gap_mana_p1 \ 0 Illinois 1.043796e+03 135.233733 4.434634e+02 -13.376365 1 Iowa 1.708954e+03 164.128524 1.036752e+03 -7.717230 2 Minnesota 9.963629e+01 -1.026561 1.694531e+02 0.000000 3 Indiana 3.994953e+02 116.336642 6.513822e+01 0.000000 4 Nebraska 5.143389e+06 42.076218 4.450334e+06 49.153631 5 Missouri 1.132276e+07 133922.054195 4.795449e+06 24.586657 6 Kansas 1.015887e+02 -13.054539 3.398922e+02 -86.786803 7 Arkansas 5.713565e+00 -52.297516 2.685377e-01 -20.312419 8 Mississippi 5.582976e+07 -67.057023 1.195978e+07 0.000000 9 Kentucky 3.258149e+03 -44.170701 4.856751e+02 -5.828260 10 Tennessee 1.027376e+03 -60.055161 2.069987e+02 -8.360729 11 North Carolina 2.979825e+02 -57.156942 5.124197e+01 -1.695715 12 Louisiana 4.264848e+09 0.000000 5.488986e+08 0.000000 13 Virginia 8.556459e+02 -89.662028 9.114176e+01 -2.755235 14 Maryland 3.687935e+02 -97.649455 1.108702e+01 -0.082781 15 Alabama 3.423714e+02 -82.751521 0.000000e+00 -0.613597 16 South Carolina 5.025051e+00 -9.476163 2.080708e+00 -0.005217 17 Oklahoma inf NaN inf NaN 18 Georgia 2.141964e+01 -24.297409 0.000000e+00 -4.226071 19 Delaware 1.888997e+01 -11.239485 3.206517e+01 -22.626940 20 Texas inf NaN inf NaN 21 New Jersey 1.818986e+07 0.000000 NaN NaN 22 Florida 1.386202e+03 0.002295 NaN NaN 23 Illinois 1.625041e+03 42.506749 1.369912e+03 -17.282037 24 Iowa 3.214051e+03 60.262151 2.689806e+03 -9.745817 25 Indiana 4.206778e+03 192.837104 6.529829e+02 -1.225652 26 Nebraska 1.328356e+07 93.822796 1.858110e+07 402.624402 27 Ohio 9.476268e+02 -2.195599 2.035779e+02 -0.437035 28 Missouri 7.917785e+06 44.254490 8.626931e+06 62.140922 29 Kansas 1.015887e+02 -13.054539 3.523071e+02 -86.930621 30 Arkansas 4.311581e+00 -48.045948 9.433064e-01 -20.699935 31 Mississippi 5.386127e+07 -67.057023 1.339936e+07 0.000000 32 Kentucky 6.679574e+03 -89.013281 1.771001e+03 -10.244621 33 Tennessee 6.312447e+02 -55.002153 4.844223e+02 -36.430755 34 North Carolina 4.667735e+02 -79.566212 5.232758e+01 -2.467429 35 Louisiana 3.066631e+09 0.000000 1.737642e+09 0.000000 36 Pennsylvania 4.414800e+03 -24.472561 9.057808e+02 -5.598919 37 Virginia 9.021434e+02 -78.938746 1.014195e+02 -3.591091 38 Maryland 5.066259e+02 -99.012095 3.056769e+01 -0.817052 39 Alabama 3.428275e+02 -76.774977 5.121938e+00 -0.613597 40 South Carolina 3.655088e+02 -44.048424 4.381500e+01 -27.391644 41 Oklahoma inf NaN inf NaN 42 Georgia 9.446390e+01 -78.026423 4.269414e+00 -12.603192 43 Delaware 1.888997e+01 -1.155979 3.206517e+01 -22.626940 44 Texas inf NaN inf NaN 45 New Jersey 2.297027e+07 39.690336 NaN NaN 46 West Virginia 4.884962e+05 -86.067678 NaN NaN 47 Florida 2.054254e+03 0.000000 NaN NaN std_p std_mana_p med_p med_mana_p obs_t \ 0 4.092978e+02 53.399063 1.490372e+03 121.856783 8.982157e-02 1 2.332553e+02 78.175063 2.736642e+03 156.411294 1.635207e-02 2 5.669471e+01 0.480835 2.165055e+02 -1.026561 4.104377e-02 3 5.149058e+01 49.536494 4.645659e+02 116.336642 2.246766e-02 4 6.536375e+05 0.000000 9.593560e+06 91.229849 3.799619e-06 5 2.935666e+05 154329.027338 1.608888e+07 133946.640852 1.095012e-05 6 4.240326e+00 0.955573 4.414809e+02 -99.841342 2.619782e-01 7 1.431300e-01 0.093713 5.982102e+00 -72.609935 1.305501e+00 8 0.000000e+00 0.000000 6.778955e+07 -67.057023 1.008715e-06 9 5.083156e+01 6.645514 3.743824e+03 -49.998961 6.512101e-03 10 3.184954e+01 41.588654 1.234374e+03 -68.415890 3.759947e-02 11 1.437182e+01 1.583892 3.492245e+02 -58.852657 8.682545e-02 12 0.000000e+00 0.000000 4.813747e+09 0.000000 7.858539e-09 13 1.667886e+01 0.806584 9.467876e+02 -92.417264 1.527527e-02 14 1.596012e+01 0.408288 3.798805e+02 -97.732237 2.575924e-02 15 7.119504e+00 14.088953 3.423714e+02 -83.365118 2.672979e-02 16 3.869340e+00 0.371992 7.105758e+00 -9.481380 2.646569e-02 17 NaN NaN inf NaN 0.000000e+00 18 2.127418e+00 2.825839 2.152856e+01 -28.523480 4.250739e-02 19 5.593430e+00 6.192771 5.095514e+01 -33.866425 3.923567e-02 20 NaN NaN inf NaN 0.000000e+00 21 3.491660e+06 0.000000 1.818986e+07 0.000000 7.452582e-08 22 3.628403e+01 0.000103 1.386202e+03 0.002295 2.030791e-04 23 1.550508e+02 51.557614 2.996290e+03 25.224713 8.982157e-02 24 5.077783e+02 70.431752 5.892938e+03 50.704246 1.635207e-02 25 2.801214e+02 130.817904 4.855034e+03 191.611452 2.246766e-02 26 1.621972e+06 23.031986 3.184267e+07 496.447198 3.799619e-06 27 1.220866e+02 49.917364 1.151205e+03 -2.632635 6.057680e-03 28 1.893995e+04 0.000000 1.654472e+07 106.395412 1.095012e-05 29 0.000000e+00 0.000000 4.538958e+02 -99.985160 2.619782e-01 30 1.861700e-01 0.000000 5.254887e+00 -68.745882 1.305501e+00 31 4.759789e+04 0.000000 6.726063e+07 -67.057023 1.008715e-06 32 3.897296e+01 0.078814 8.450576e+03 -99.257902 6.512101e-03 33 2.468632e+01 1.487110 1.115667e+03 -91.432907 3.759947e-02 34 2.275456e+01 8.143243 5.191010e+02 -82.033642 8.682545e-02 35 0.000000e+00 0.000000 4.804273e+09 0.000000 7.858539e-09 36 1.572277e+03 823.676739 5.315352e+03 -30.071480 6.010994e-04 37 3.960080e-01 0.107115 1.003563e+03 -82.529837 1.527527e-02 38 1.560528e+01 12.057114 5.371936e+02 -99.829147 2.575924e-02 39 1.438383e+01 2.232195 3.479494e+02 -77.388573 2.672979e-02 40 1.462294e+01 1.143951 4.093238e+02 -71.440068 2.646569e-02 41 NaN NaN inf NaN 0.000000e+00 42 1.598527e+00 0.737217 9.873332e+01 -90.629615 4.250739e-02 43 0.000000e+00 16.357262 5.095514e+01 -23.782919 3.923567e-02 44 NaN NaN inf NaN 0.000000e+00 45 2.170629e+05 5.899259 2.297027e+07 39.690336 7.452582e-08 46 6.304528e+03 0.000000 4.884962e+05 -86.067678 6.815925e-07 47 3.795199e+01 16.213647 2.054254e+03 0.000000 2.030791e-04 ... gap_mana_p95 gap_p5 gap_mana_p5 gap_unsus_p95 \ 0 ... 211.436917 9.367749e+02 40.855749 7.212433e+02 1 ... 315.676226 2.346667e+03 32.189352 1.096662e+03 2 ... 0.000000 1.345447e+02 -1.026561 1.694531e+02 3 ... 154.945382 4.362338e+02 -2.528470 6.513822e+01 4 ... 91.229849 8.029734e+06 91.229849 5.058865e+06 5 ... 444591.037909 1.561322e+07 91.364592 4.795449e+06 6 ... -99.841342 4.334456e+02 -99.841342 3.440696e+02 7 ... -72.235305 5.501694e+00 -72.609935 2.685377e-01 8 ... -67.057023 6.778955e+07 -67.057023 1.195978e+07 9 ... -49.998961 3.628029e+03 -49.998961 4.856751e+02 10 ... 30.884253 1.158931e+03 -98.382162 2.069987e+02 11 ... -55.425584 3.225823e+02 -59.970057 5.124197e+01 12 ... 0.000000 4.813747e+09 0.000000 5.488986e+08 13 ... -90.301399 9.099152e+02 -92.417264 9.114176e+01 14 ... -97.584037 3.569297e+02 -97.732237 1.108702e+01 15 ... -56.363709 3.335580e+02 -86.651770 0.000000e+00 16 ... -9.356845 7.105758e+00 -9.481380 2.080708e+00 17 ... NaN NaN NaN NaN 18 ... -24.925953 1.951379e+01 -32.646879 7.367597e-01 19 ... -28.834839 3.206517e+01 -46.121930 3.206517e+01 20 ... NaN NaN NaN NaN 21 ... 0.000000 4.820710e+06 0.000000 NaN 22 ... 0.002295 1.386202e+03 0.002295 NaN 23 ... 119.621044 2.723989e+03 -45.425468 NaN 24 ... 177.291785 4.960528e+03 -44.439554 NaN 25 ... 389.656960 4.314155e+03 -16.079952 NaN 26 ... 496.447198 2.928036e+07 420.603925 NaN 27 ... 121.715892 9.351686e+02 -2.642114 NaN 28 ... 106.395412 1.649127e+07 106.395412 NaN 29 ... -99.985160 4.538958e+02 -99.985160 NaN 30 ... -68.745882 5.171751e+00 -68.745882 NaN 31 ... -67.057023 6.726063e+07 -67.057023 NaN 32 ... -99.100098 8.372821e+03 -99.257902 NaN 33 ... -90.107557 1.083400e+03 -92.629641 NaN 34 ... -55.885008 4.490003e+02 -82.783639 NaN 35 ... 0.000000 4.804273e+09 0.000000 NaN 36 ... 2051.278191 3.380111e+03 -39.296993 NaN 37 ... -82.529837 1.003563e+03 -82.529837 NaN 38 ... -70.066663 4.909990e+02 -99.981830 NaN 39 ... -72.012252 3.129124e+02 -77.444104 NaN 40 ... -66.968275 3.561617e+02 -71.440068 NaN 41 ... NaN NaN NaN NaN 42 ... -89.130810 9.489853e+01 -90.629615 NaN 43 ... -23.782919 5.095514e+01 -69.842263 NaN 44 ... NaN NaN NaN NaN 45 ... 39.690336 2.297027e+07 39.690336 NaN 46 ... -86.067678 4.884962e+05 -86.067678 NaN 47 ... 0.000000 2.054254e+03 0.000000 NaN gap_mana_unsus_p95 gap_unsus_p5 gap_mana_unsus_p5 p_total zero \ 0 -13.376365 2.257986e+02 -13.376365 1.487259e+03 0 1 -7.704523 7.491450e+02 -7.717230 2.745706e+03 0 2 0.000000 1.694531e+02 0.000000 2.690894e+02 0 3 0.000000 6.513822e+01 0.000000 4.646335e+02 0 4 49.153631 2.886345e+06 49.153631 9.593723e+06 0 5 24.586657 4.675879e+06 24.586657 1.611821e+07 0 6 -86.786803 3.318570e+02 -86.786803 4.414809e+02 0 7 -20.312419 2.685377e-01 -20.312419 5.982102e+00 0 8 0.000000 1.195978e+07 0.000000 6.778955e+07 0 9 -5.828260 4.856751e+02 -5.828260 3.743824e+03 0 10 -8.360729 1.930302e+02 -8.360729 1.234374e+03 0 11 -1.695715 5.124197e+01 -1.695715 3.492245e+02 0 12 0.000000 5.488986e+08 0.000000 4.813747e+09 0 13 -2.755235 8.292991e+01 -2.755235 9.467876e+02 0 14 -0.082781 1.108702e+01 -0.082781 3.798805e+02 0 15 -0.613597 0.000000e+00 -0.613597 3.423714e+02 0 16 -0.005217 2.080708e+00 -0.005217 7.105758e+00 0 17 NaN NaN NaN inf 0 18 -4.226071 0.000000e+00 -4.226071 2.141964e+01 0 19 -22.626940 3.206517e+01 -22.626940 5.095514e+01 0 20 NaN NaN NaN inf 0 21 NaN NaN NaN NaN 0 22 NaN NaN NaN NaN 0 23 NaN NaN NaN 2.994952e+03 0 24 NaN NaN NaN 5.903857e+03 0 25 NaN NaN NaN 4.859761e+03 0 26 NaN NaN NaN 3.186466e+07 0 27 NaN NaN NaN 1.151205e+03 0 28 NaN NaN NaN 1.654472e+07 0 29 NaN NaN NaN 4.538958e+02 0 30 NaN NaN NaN 5.254887e+00 0 31 NaN NaN NaN 6.726063e+07 0 32 NaN NaN NaN 8.450576e+03 0 33 NaN NaN NaN 1.115667e+03 0 34 NaN NaN NaN 5.191010e+02 0 35 NaN NaN NaN 4.804273e+09 0 36 NaN NaN NaN 5.320581e+03 0 37 NaN NaN NaN 1.003563e+03 0 38 NaN NaN NaN 5.371936e+02 0 39 NaN NaN NaN 3.479494e+02 0 40 NaN NaN NaN 4.093238e+02 0 41 NaN NaN NaN inf 0 42 NaN NaN NaN 9.873332e+01 0 43 NaN NaN NaN 5.095514e+01 0 44 NaN NaN NaN inf 0 45 NaN NaN NaN NaN 0 46 NaN NaN NaN NaN 0 47 NaN NaN NaN NaN 0 scenario 0 1.5 1 1.5 2 1.5 3 1.5 4 1.5 5 1.5 6 1.5 7 1.5 8 1.5 9 1.5 10 1.5 11 1.5 12 1.5 13 1.5 14 1.5 15 1.5 16 1.5 17 1.5 18 1.5 19 1.5 20 1.5 21 1.5 22 1.5 23 3.0 24 3.0 25 3.0 26 3.0 27 3.0 28 3.0 29 3.0 30 3.0 31 3.0 32 3.0 33 3.0 34 3.0 35 3.0 36 3.0 37 3.0 38 3.0 39 3.0 40 3.0 41 3.0 42 3.0 43 3.0 44 3.0 45 3.0 46 3.0 47 3.0 [48 rows x 22 columns] std_p std_mana_p irri_p_med irri_mana_p_med irri_p5 \ 0 21.704200 2.825661 372.007492 -56.240573 340.086379 1 15.426849 2.680809 575.965738 -61.453047 549.903058 irri_mana_p5 irri_p95 irri_mana_p95 irri_mana_unsus_p5 \ 0 -60.568632 413.691214 -51.706090 -24.370790 1 -65.610339 599.279094 -57.020825 -25.875315 irri_mana_unsus_p95 irri_mana_unsus_med irri_unsus_p5 irri_unsus_p95 \ 0 -24.370693 -24.370790 111.606368 137.056116 1 -25.871326 -25.875315 232.049703 249.687643 irri_unsus_med STATE1 scenario 0 123.727496 U.S. 1.5 1 241.962030 U.S. 3.0
err=err[['STATE1','med_p','gap_p5', 'gap_p95',
'med_mana_p','gap_mana_p5', 'gap_mana_p95','scenario']]
err['p95']=np.abs(err['gap_mana_p5']-err['med_p'])
err['p5']=np.abs(err['gap_p5']-err['med_p'])
err['p95_mana']=np.abs(err['gap_mana_p95']-err['med_mana_p'])
err['p5_mana']=np.abs(err['gap_mana_p5']-err['med_mana_p'])
print(err)
err=err[['STATE1','p95', 'p5','p95_mana','p5_mana','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.rename(columns={'p95_mana': 'p95_mana_15'}, inplace=True)
err15.rename(columns={'p5_mana': 'p5_mana_15'}, inplace=True)
err15=err15[['STATE1','p95_15', 'p5_15','p5_mana_15','p95_mana_15']]
err3.rename(columns={'p95': 'p95_3'}, inplace=True)
err3.rename(columns={'p5': 'p5_3'}, inplace=True)
err3.rename(columns={'p95_mana': 'p95_mana_3'}, inplace=True)
err3.rename(columns={'p5_mana': 'p5_mana_3'}, inplace=True)
err3=err3[['STATE1','p95_3', 'p5_3','p95_mana_3','p5_mana_3']]
err = err15.merge(err3, on='STATE1', how='left')
print(err)
err_country=err_country[['STATE1','irri_p_med','irri_p5','irri_p95',
'irri_mana_p_med','irri_mana_p5', 'irri_mana_p95','scenario']]
err_country['p95']=np.abs(err_country['irri_p_med']-err_country['irri_p95'])
err_country['p5']=np.abs(err_country['irri_p_med']-err_country['irri_p5'])
err_country['p95_mana']=np.abs(err_country['irri_mana_p_med']-err_country['irri_mana_p95'])
err_country['p5_mana']=np.abs(err_country['irri_mana_p_med']-err_country['irri_mana_p5'])
err_country=err_country[['STATE1','p95', 'p5','p95_mana','p5_mana','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.rename(columns={'p95_mana': 'p95_mana_15'}, inplace=True)
err_country15.rename(columns={'p5_mana': 'p5_mana_15'}, inplace=True)
err_country15=err_country15[['STATE1','p95_15', 'p5_15','p95_mana_15','p5_mana_15']]
err_country3.rename(columns={'p95': 'p95_3'}, inplace=True)
err_country3.rename(columns={'p5': 'p5_3'}, inplace=True)
err_country3.rename(columns={'p95_mana': 'p95_mana_3'}, inplace=True)
err_country3.rename(columns={'p5_mana': 'p5_mana_3'}, inplace=True)
err_country3=err_country3[['STATE1','p95_3', 'p5_3','p95_mana_3','p5_mana_3']]
err_country = err_country15.merge(err_country3, on='STATE1', how='left')
print(err_country)
STATE1 med_p gap_p5 gap_p95 med_mana_p \ 0 Illinois 1.490372e+03 9.367749e+02 2.328450e+03 121.856783 1 Iowa 2.736642e+03 2.346667e+03 3.114363e+03 156.411294 2 Minnesota 2.165055e+02 1.345447e+02 2.690894e+02 -1.026561 3 Indiana 4.645659e+02 4.362338e+02 5.771691e+02 116.336642 4 Nebraska 9.593560e+06 8.029734e+06 1.020225e+07 91.229849 5 Missouri 1.608888e+07 1.561322e+07 1.640775e+07 133946.640852 6 Kansas 4.414809e+02 4.334456e+02 4.456583e+02 -99.841342 7 Arkansas 5.982102e+00 5.501694e+00 6.156430e+00 -72.609935 8 Mississippi 6.778955e+07 6.778955e+07 6.778955e+07 -67.057023 9 Kentucky 3.743824e+03 3.628029e+03 3.743824e+03 -49.998961 10 Tennessee 1.234374e+03 1.158931e+03 1.243626e+03 -68.415890 11 North Carolina 3.492245e+02 3.225823e+02 3.734190e+02 -58.852657 12 Louisiana 4.813747e+09 4.813747e+09 4.813747e+09 0.000000 13 Virginia 9.467876e+02 9.099152e+02 9.505188e+02 -92.417264 14 Maryland 3.798805e+02 3.569297e+02 4.039881e+02 -97.732237 15 Alabama 3.423714e+02 3.335580e+02 3.591185e+02 -83.365118 16 South Carolina 7.105758e+00 7.105758e+00 7.105758e+00 -9.481380 17 Oklahoma inf NaN NaN NaN 18 Georgia 2.152856e+01 1.951379e+01 2.609677e+01 -28.523480 19 Delaware 5.095514e+01 3.206517e+01 5.095514e+01 -33.866425 20 Texas inf NaN NaN NaN 21 New Jersey 1.818986e+07 4.820710e+06 1.818986e+07 0.000000 22 Florida 1.386202e+03 1.386202e+03 1.386202e+03 0.002295 23 Illinois 2.996290e+03 2.723989e+03 3.234991e+03 25.224713 24 Iowa 5.892938e+03 4.960528e+03 6.655936e+03 50.704246 25 Indiana 4.855034e+03 4.314155e+03 5.227811e+03 191.611452 26 Nebraska 3.184267e+07 2.928036e+07 3.422605e+07 496.447198 27 Ohio 1.151205e+03 9.351686e+02 1.294429e+03 -2.632635 28 Missouri 1.654472e+07 1.649127e+07 1.654472e+07 106.395412 29 Kansas 4.538958e+02 4.538958e+02 4.538958e+02 -99.985160 30 Arkansas 5.254887e+00 5.171751e+00 5.611864e+00 -68.745882 31 Mississippi 6.726063e+07 6.726063e+07 6.737731e+07 -67.057023 32 Kentucky 8.450576e+03 8.372821e+03 8.450576e+03 -99.257902 33 Tennessee 1.115667e+03 1.083400e+03 1.138758e+03 -91.432907 34 North Carolina 5.191010e+02 4.490003e+02 5.220010e+02 -82.033642 35 Louisiana 4.804273e+09 4.804273e+09 4.804273e+09 0.000000 36 Pennsylvania 5.315352e+03 3.380111e+03 7.142181e+03 -30.071480 37 Virginia 1.003563e+03 1.003563e+03 1.003563e+03 -82.529837 38 Maryland 5.371936e+02 4.909990e+02 5.371936e+02 -99.829147 39 Alabama 3.479494e+02 3.129124e+02 3.506977e+02 -77.388573 40 South Carolina 4.093238e+02 3.561617e+02 4.106975e+02 -71.440068 41 Oklahoma inf NaN NaN NaN 42 Georgia 9.873332e+01 9.489853e+01 9.873332e+01 -90.629615 43 Delaware 5.095514e+01 5.095514e+01 5.095514e+01 -23.782919 44 Texas inf NaN NaN NaN 45 New Jersey 2.297027e+07 2.297027e+07 2.297027e+07 39.690336 46 West Virginia 4.884962e+05 4.884962e+05 4.884962e+05 -86.067678 47 Florida 2.054254e+03 2.054254e+03 2.054254e+03 0.000000 gap_mana_p5 gap_mana_p95 scenario p95 p5 \ 0 40.855749 211.436917 1.5 1.449517e+03 5.535976e+02 1 32.189352 315.676226 1.5 2.704453e+03 3.899751e+02 2 -1.026561 0.000000 1.5 2.175321e+02 8.196079e+01 3 -2.528470 154.945382 1.5 4.670944e+02 2.833211e+01 4 91.229849 91.229849 1.5 9.593469e+06 1.563826e+06 5 91.364592 444591.037909 1.5 1.608879e+07 4.756684e+05 6 -99.841342 -99.841342 1.5 5.413222e+02 8.035247e+00 7 -72.609935 -72.235305 1.5 7.859204e+01 4.804079e-01 8 -67.057023 -67.057023 1.5 6.778961e+07 0.000000e+00 9 -49.998961 -49.998961 1.5 3.793823e+03 1.157950e+02 10 -98.382162 30.884253 1.5 1.332756e+03 7.544312e+01 11 -59.970057 -55.425584 1.5 4.091946e+02 2.664222e+01 12 0.000000 0.000000 1.5 4.813747e+09 0.000000e+00 13 -92.417264 -90.301399 1.5 1.039205e+03 3.687244e+01 14 -97.732237 -97.584037 1.5 4.776128e+02 2.295084e+01 15 -86.651770 -56.363709 1.5 4.290232e+02 8.813377e+00 16 -9.481380 -9.356845 1.5 1.658714e+01 0.000000e+00 17 NaN NaN 1.5 NaN NaN 18 -32.646879 -24.925953 1.5 5.417544e+01 2.014772e+00 19 -46.121930 -28.834839 1.5 9.707707e+01 1.888997e+01 20 NaN NaN 1.5 NaN NaN 21 0.000000 0.000000 1.5 1.818986e+07 1.336915e+07 22 0.002295 0.002295 1.5 1.386199e+03 0.000000e+00 23 -45.425468 119.621044 3.0 3.041716e+03 2.723007e+02 24 -44.439554 177.291785 3.0 5.937378e+03 9.324097e+02 25 -16.079952 389.656960 3.0 4.871114e+03 5.408793e+02 26 420.603925 496.447198 3.0 3.184225e+07 2.562310e+06 27 -2.642114 121.715892 3.0 1.153847e+03 2.160361e+02 28 106.395412 106.395412 3.0 1.654461e+07 5.344353e+04 29 -99.985160 -99.985160 3.0 5.538810e+02 0.000000e+00 30 -68.745882 -68.745882 3.0 7.400077e+01 8.313594e-02 31 -67.057023 -67.057023 3.0 6.726070e+07 0.000000e+00 32 -99.257902 -99.100098 3.0 8.549834e+03 7.775513e+01 33 -92.629641 -90.107557 3.0 1.208297e+03 3.226699e+01 34 -82.783639 -55.885008 3.0 6.018847e+02 7.010078e+01 35 0.000000 0.000000 3.0 4.804273e+09 0.000000e+00 36 -39.296993 2051.278191 3.0 5.354649e+03 1.935241e+03 37 -82.529837 -82.529837 3.0 1.086093e+03 0.000000e+00 38 -99.981830 -70.066663 3.0 6.371754e+02 4.619459e+01 39 -77.444104 -72.012252 3.0 4.253935e+02 3.503701e+01 40 -71.440068 -66.968275 3.0 4.807638e+02 5.316203e+01 41 NaN NaN 3.0 NaN NaN 42 -90.629615 -89.130810 3.0 1.893629e+02 3.834790e+00 43 -69.842263 -23.782919 3.0 1.207974e+02 0.000000e+00 44 NaN NaN 3.0 NaN NaN 45 39.690336 39.690336 3.0 2.297023e+07 0.000000e+00 46 -86.067678 -86.067678 3.0 4.885822e+05 0.000000e+00 47 0.000000 0.000000 3.0 2.054254e+03 0.000000e+00 p95_mana p5_mana 0 89.580134 81.001034 1 159.264931 124.221942 2 1.026561 0.000000 3 38.608740 118.865112 4 0.000000 0.000000 5 310644.397057 133855.276260 6 0.000000 0.000000 7 0.374630 0.000000 8 0.000000 0.000000 9 0.000000 0.000000 10 99.300143 29.966272 11 3.427073 1.117400 12 0.000000 0.000000 13 2.115864 0.000000 14 0.148200 0.000000 15 27.001409 3.286652 16 0.124535 0.000000 17 NaN NaN 18 3.597527 4.123399 19 5.031586 12.255505 20 NaN NaN 21 0.000000 0.000000 22 0.000000 0.000000 23 94.396331 70.650181 24 126.587539 95.143800 25 198.045508 207.691404 26 0.000000 75.843273 27 124.348527 0.009479 28 0.000000 0.000000 29 0.000000 0.000000 30 0.000000 0.000000 31 0.000000 0.000000 32 0.157804 0.000000 33 1.325350 1.196734 34 26.148634 0.749997 35 0.000000 0.000000 36 2081.349670 9.225513 37 0.000000 0.000000 38 29.762485 0.152683 39 5.376322 0.055530 40 4.471793 0.000000 41 NaN NaN 42 1.498805 0.000000 43 0.000000 46.059345 44 NaN NaN 45 0.000000 0.000000 46 0.000000 0.000000 47 0.000000 0.000000 STATE1 p95 p5 p95_mana p5_mana \ 0 Illinois 1.449517e+03 5.535976e+02 89.580134 81.001034 1 Iowa 2.704453e+03 3.899751e+02 159.264931 124.221942 2 Minnesota 2.175321e+02 8.196079e+01 1.026561 0.000000 3 Indiana 4.670944e+02 2.833211e+01 38.608740 118.865112 4 Nebraska 9.593469e+06 1.563826e+06 0.000000 0.000000 5 Missouri 1.608879e+07 4.756684e+05 310644.397057 133855.276260 6 Kansas 5.413222e+02 8.035247e+00 0.000000 0.000000 7 Arkansas 7.859204e+01 4.804079e-01 0.374630 0.000000 8 Mississippi 6.778961e+07 0.000000e+00 0.000000 0.000000 9 Kentucky 3.793823e+03 1.157950e+02 0.000000 0.000000 10 Tennessee 1.332756e+03 7.544312e+01 99.300143 29.966272 11 North Carolina 4.091946e+02 2.664222e+01 3.427073 1.117400 12 Louisiana 4.813747e+09 0.000000e+00 0.000000 0.000000 13 Virginia 1.039205e+03 3.687244e+01 2.115864 0.000000 14 Maryland 4.776128e+02 2.295084e+01 0.148200 0.000000 15 Alabama 4.290232e+02 8.813377e+00 27.001409 3.286652 16 South Carolina 1.658714e+01 0.000000e+00 0.124535 0.000000 17 Oklahoma NaN NaN NaN NaN 18 Georgia 5.417544e+01 2.014772e+00 3.597527 4.123399 19 Delaware 9.707707e+01 1.888997e+01 5.031586 12.255505 20 Texas NaN NaN NaN NaN 21 New Jersey 1.818986e+07 1.336915e+07 0.000000 0.000000 22 Florida 1.386199e+03 0.000000e+00 0.000000 0.000000 23 Illinois 3.041716e+03 2.723007e+02 94.396331 70.650181 24 Iowa 5.937378e+03 9.324097e+02 126.587539 95.143800 25 Indiana 4.871114e+03 5.408793e+02 198.045508 207.691404 26 Nebraska 3.184225e+07 2.562310e+06 0.000000 75.843273 27 Ohio 1.153847e+03 2.160361e+02 124.348527 0.009479 28 Missouri 1.654461e+07 5.344353e+04 0.000000 0.000000 29 Kansas 5.538810e+02 0.000000e+00 0.000000 0.000000 30 Arkansas 7.400077e+01 8.313594e-02 0.000000 0.000000 31 Mississippi 6.726070e+07 0.000000e+00 0.000000 0.000000 32 Kentucky 8.549834e+03 7.775513e+01 0.157804 0.000000 33 Tennessee 1.208297e+03 3.226699e+01 1.325350 1.196734 34 North Carolina 6.018847e+02 7.010078e+01 26.148634 0.749997 35 Louisiana 4.804273e+09 0.000000e+00 0.000000 0.000000 36 Pennsylvania 5.354649e+03 1.935241e+03 2081.349670 9.225513 37 Virginia 1.086093e+03 0.000000e+00 0.000000 0.000000 38 Maryland 6.371754e+02 4.619459e+01 29.762485 0.152683 39 Alabama 4.253935e+02 3.503701e+01 5.376322 0.055530 40 South Carolina 4.807638e+02 5.316203e+01 4.471793 0.000000 41 Oklahoma NaN NaN NaN NaN 42 Georgia 1.893629e+02 3.834790e+00 1.498805 0.000000 43 Delaware 1.207974e+02 0.000000e+00 0.000000 46.059345 44 Texas NaN NaN NaN NaN 45 New Jersey 2.297023e+07 0.000000e+00 0.000000 0.000000 46 West Virginia 4.885822e+05 0.000000e+00 0.000000 0.000000 47 Florida 2.054254e+03 0.000000e+00 0.000000 0.000000 scenario 0 1.5 1 1.5 2 1.5 3 1.5 4 1.5 5 1.5 6 1.5 7 1.5 8 1.5 9 1.5 10 1.5 11 1.5 12 1.5 13 1.5 14 1.5 15 1.5 16 1.5 17 1.5 18 1.5 19 1.5 20 1.5 21 1.5 22 1.5 23 3.0 24 3.0 25 3.0 26 3.0 27 3.0 28 3.0 29 3.0 30 3.0 31 3.0 32 3.0 33 3.0 34 3.0 35 3.0 36 3.0 37 3.0 38 3.0 39 3.0 40 3.0 41 3.0 42 3.0 43 3.0 44 3.0 45 3.0 46 3.0 47 3.0 STATE1 p95_15 p5_15 p5_mana_15 p95_mana_15 \ 0 Illinois 1.449517e+03 5.535976e+02 81.001034 89.580134 1 Iowa 2.704453e+03 3.899751e+02 124.221942 159.264931 2 Minnesota 2.175321e+02 8.196079e+01 0.000000 1.026561 3 Indiana 4.670944e+02 2.833211e+01 118.865112 38.608740 4 Nebraska 9.593469e+06 1.563826e+06 0.000000 0.000000 5 Missouri 1.608879e+07 4.756684e+05 133855.276260 310644.397057 6 Kansas 5.413222e+02 8.035247e+00 0.000000 0.000000 7 Arkansas 7.859204e+01 4.804079e-01 0.000000 0.374630 8 Mississippi 6.778961e+07 0.000000e+00 0.000000 0.000000 9 Kentucky 3.793823e+03 1.157950e+02 0.000000 0.000000 10 Tennessee 1.332756e+03 7.544312e+01 29.966272 99.300143 11 North Carolina 4.091946e+02 2.664222e+01 1.117400 3.427073 12 Louisiana 4.813747e+09 0.000000e+00 0.000000 0.000000 13 Virginia 1.039205e+03 3.687244e+01 0.000000 2.115864 14 Maryland 4.776128e+02 2.295084e+01 0.000000 0.148200 15 Alabama 4.290232e+02 8.813377e+00 3.286652 27.001409 16 South Carolina 1.658714e+01 0.000000e+00 0.000000 0.124535 17 Oklahoma NaN NaN NaN NaN 18 Georgia 5.417544e+01 2.014772e+00 4.123399 3.597527 19 Delaware 9.707707e+01 1.888997e+01 12.255505 5.031586 20 Texas NaN NaN NaN NaN 21 New Jersey 1.818986e+07 1.336915e+07 0.000000 0.000000 22 Florida 1.386199e+03 0.000000e+00 0.000000 0.000000 p95_3 p5_3 p95_mana_3 p5_mana_3 0 3.041716e+03 2.723007e+02 94.396331 70.650181 1 5.937378e+03 9.324097e+02 126.587539 95.143800 2 NaN NaN NaN NaN 3 4.871114e+03 5.408793e+02 198.045508 207.691404 4 3.184225e+07 2.562310e+06 0.000000 75.843273 5 1.654461e+07 5.344353e+04 0.000000 0.000000 6 5.538810e+02 0.000000e+00 0.000000 0.000000 7 7.400077e+01 8.313594e-02 0.000000 0.000000 8 6.726070e+07 0.000000e+00 0.000000 0.000000 9 8.549834e+03 7.775513e+01 0.157804 0.000000 10 1.208297e+03 3.226699e+01 1.325350 1.196734 11 6.018847e+02 7.010078e+01 26.148634 0.749997 12 4.804273e+09 0.000000e+00 0.000000 0.000000 13 1.086093e+03 0.000000e+00 0.000000 0.000000 14 6.371754e+02 4.619459e+01 29.762485 0.152683 15 4.253935e+02 3.503701e+01 5.376322 0.055530 16 4.807638e+02 5.316203e+01 4.471793 0.000000 17 NaN NaN NaN NaN 18 1.893629e+02 3.834790e+00 1.498805 0.000000 19 1.207974e+02 0.000000e+00 0.000000 46.059345 20 NaN NaN NaN NaN 21 2.297023e+07 0.000000e+00 0.000000 0.000000 22 2.054254e+03 0.000000e+00 0.000000 0.000000 STATE1 p95 p5 p95_mana p5_mana scenario 0 U.S. 41.683722 31.921113 4.534483 4.328059 1.5 1 U.S. 23.313356 26.062680 4.432222 4.157292 3.0 STATE1 p95_15 p5_15 p95_mana_15 p5_mana_15 p95_3 p5_3 \ 0 U.S. 41.683722 31.921113 4.534483 4.328059 23.313356 26.06268 p95_mana_3 p5_mana_3 0 4.432222 4.157292
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.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 err15.rename(columns={'p95': 'p95_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:13: 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_14618/4253460213.py:14: 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_mana': 'p95_mana_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:15: 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_mana': 'p5_mana_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:17: 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_14618/4253460213.py:18: 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_14618/4253460213.py:19: 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_mana': 'p95_mana_3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:20: 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_mana': 'p5_mana_3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:36: 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_14618/4253460213.py:37: 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_14618/4253460213.py:38: 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_mana': 'p95_mana_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:39: 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_mana': 'p5_mana_15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:41: 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_14618/4253460213.py:42: 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) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:43: 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_mana': 'p95_mana_3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/4253460213.py:44: 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_mana': 'p5_mana_3'}, inplace=True)
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Figure 6 sustain and unsustain/mean_sus_unsus15.csv"
mean_sus_unsus15 = pd.read_csv(excel_file)
print(mean_sus_unsus15)
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Figure 6 sustain and unsustain/mean_sus_unsus3.csv"
mean_sus_unsus3 = pd.read_csv(excel_file)
print(mean_sus_unsus3)
GEOID sus_unsus 0 1001 0 1 1003 0 2 1005 0 3 1007 0 4 1009 0 ... ... ... 1992 56037 1 1993 56039 0 1994 56041 1 1995 56043 1 1996 56045 1 [1997 rows x 2 columns] GEOID sus_unsus 0 1001 0.0 1 28031 0.0 2 28037 0.0 3 28041 0.0 4 28045 0.0 ... ... ... 1992 29121 1.0 1993 29115 1.0 1994 29107 1.0 1995 29145 1.0 1996 56045 1.0 [1997 rows x 2 columns]
# sustain and unsustain under 1.5 warming scenairo
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Figure 3 Yiled under warming scenario/off_soybean_mana_lnfer_irri.csv"
data1 = pd.read_csv(excel_file)
data1 = data1[data1['scenario'] == 1.5]
data1.rename(columns={'geoid': 'GEOID'}, inplace=True)
data1['GEOID'] = data1['GEOID'].astype(int)
print(data1)
# Convert 'GEOID' column to int64 in gazetteer_data
gazetteer_data['GEOID'] = gazetteer_data['GEOID'].astype(int)
mean_sus_unsus15['GEOID'] = mean_sus_unsus15['GEOID'].astype(int)
data=data1.merge(gazetteer_data, on='GEOID', how='left')
data=data.merge(mean_sus_unsus15, on='GEOID', how='left')
data['irrigation_t']=(data['irri_need_1']-data['irrigation1'])*data['har']/1000000 #km3
data['irrigation_mana_t']=data['gap_irrigation']*data['har']/1000000 #km3
# Replace NaN values in 'sus_unsus' column with 0
data['sus_unsus'].fillna(0, inplace=True)
data.rename(columns={'GEOID':'geoid'}, inplace=True)
data = data.sort_values(by='geoid')
print(data)
data.rename(columns={'irrigation_t':'irri_t'}, inplace=True)
data.rename(columns={'irrigation_mana_t':'irri_mana_t'}, inplace=True)
data15 = data[['scenario','geoid','irri_t','irri_mana_t','har','sus_unsus','STATE1']]
print(data15)
tmp pre lnfer irrigation1 y_temp mana_group GEOID \ 4 21.297634 6.267068 -0.046396 0.001389 7.763110 1 17067 6 23.368236 7.039531 2.480696 0.042775 8.064906 1 37045 8 20.636286 5.911292 1.265328 0.007367 7.910679 1 17075 11 24.389220 8.362195 1.247938 0.009316 7.846596 1 37031 13 21.414218 5.922644 0.748695 0.001327 7.844605 1 17107 ... ... ... ... ... ... ... ... 1979 23.881700 6.681210 6.281323 0.091063 8.549590 3 1059 1981 25.374776 6.487229 6.026028 0.058274 8.366090 3 1063 1983 25.375840 6.385456 6.055595 0.081476 8.357682 3 1065 1988 23.976584 6.790346 4.361742 0.056342 8.285447 3 1009 1992 23.975426 6.860577 4.369129 0.149384 8.278050 3 1043 scenario lnyield_obs scenario.1 har gap_fer \ 4 1.5 8.213926 1.5 58447.890 0.110474 6 1.5 7.648014 1.5 5522.715 0.515904 8 1.5 8.143628 1.5 108519.700 0.176216 11 1.5 7.732798 1.5 8839.125 0.143718 13 1.5 8.279940 1.5 53825.450 0.184053 ... ... ... ... ... ... 1979 1.5 7.841934 1.5 1782.427 230.462736 1981 1.5 7.382296 1.5 931.500 157.742963 1983 1.5 7.649490 1.5 1608.660 156.285995 1988 1.5 7.840701 1.5 1107.736 22.209510 1992 1.5 7.978103 1.5 2906.246 27.753527 gap_irrigation lnfer_need lnfer_values irri_need y_temp_base2015 \ 4 -0.001366 0.062503 0.063104 0.235000 7.763110 6 -0.042751 2.551202 2.522963 0.837454 8.064906 8 -0.007344 1.313912 1.313850 NaN 7.910679 11 -0.009292 1.290816 1.288370 0.837454 7.846596 13 -0.001303 0.831943 0.832166 0.455000 7.844605 ... ... ... ... ... ... 1979 0.000000 6.639821 6.639821 0.837454 8.549590 1981 0.000000 6.348807 6.348807 0.837454 8.366090 1983 0.000000 6.367807 6.367807 0.837454 8.357682 1988 0.000000 4.611183 4.611183 0.837454 8.285447 1992 0.000000 4.670287 4.670287 0.837454 8.278050 y_temp_base_t irri_need_1 y_temp_irri 4 7.748190 0.235000 7.763084 6 8.055423 0.837454 7.993940 8 7.904037 0.837454 7.903477 11 7.840749 0.837454 7.816472 13 7.833198 0.455000 7.844601 ... ... ... ... 1979 8.502215 0.837454 8.318906 1981 8.322773 0.837454 8.162960 1983 8.316101 0.837454 8.166404 1988 8.252038 0.837454 8.127237 1992 8.239287 0.837454 8.128134 [852 rows x 20 columns] tmp pre lnfer irrigation1 y_temp mana_group geoid \ 576 25.099590 6.254395 2.457656 0.377088 7.874365 2 1001 577 26.261318 9.274925 1.292553 0.738441 7.602207 2 1003 578 25.408514 6.727813 2.114730 0.028216 7.826231 2 1005 850 23.976584 6.790346 4.361742 0.056342 8.285447 3 1009 137 25.246212 6.448847 4.858469 0.030586 8.201724 1 1011 .. ... ... ... ... ... ... ... 789 23.054028 6.988594 1.451035 0.047134 7.976050 2 51183 387 22.726178 6.559806 1.287505 0.055504 7.938844 1 51193 388 23.445478 7.457666 1.553341 0.022453 7.970973 1 51550 389 23.349492 7.419639 1.187985 0.008524 7.928655 1 51800 390 23.207888 7.236578 2.482080 0.010662 8.111622 1 51810 scenario lnyield_obs scenario.1 ... y_temp_base_t irri_need_1 \ 576 1.5 7.509678 1.5 ... 7.846458 0.837454 577 1.5 7.927428 1.5 ... 7.590898 0.738441 578 1.5 8.161582 1.5 ... 7.807509 0.837454 850 1.5 7.840701 1.5 ... 8.252038 0.837454 137 1.5 7.984614 1.5 ... 8.183765 0.837454 .. ... ... ... ... ... ... 789 1.5 7.653934 1.5 ... 7.933484 0.837454 387 1.5 7.805104 1.5 ... 7.908054 0.837454 388 1.5 7.885281 1.5 ... 7.940719 0.837454 389 1.5 7.798817 1.5 ... 7.896463 0.837454 390 1.5 7.872881 1.5 ... 8.075677 0.837454 y_temp_irri USPS Region STATE1 stateansi sus_unsus \ 576 7.826731 AL E SOUTH CENTRAL Alabama 1.0 0.0 577 7.590898 AL E SOUTH CENTRAL Alabama 1.0 0.0 578 7.773008 AL E SOUTH CENTRAL Alabama 1.0 0.0 850 8.127237 AL E SOUTH CENTRAL Alabama 1.0 0.0 137 8.059327 AL E SOUTH CENTRAL Alabama 1.0 0.0 .. ... ... ... ... ... ... 789 7.915894 VA SOUTHEAST Virginia 51.0 0.0 387 7.902166 VA SOUTHEAST Virginia 51.0 0.0 388 7.911710 VA SOUTHEAST Virginia 51.0 0.0 389 7.884957 VA SOUTHEAST Virginia 51.0 0.0 390 8.005891 VA SOUTHEAST Virginia 51.0 0.0 irrigation_t irrigation_mana_t 576 0.000359 -0.000294 577 0.000000 -0.004695 578 0.000426 -0.000015 850 0.000865 0.000000 137 0.000703 -0.000027 .. ... ... 789 0.008107 -0.000483 387 0.005570 -0.000395 388 0.007986 -0.000220 389 0.006605 -0.000068 390 0.004112 -0.000053 [852 rows x 27 columns] scenario geoid irri_t irri_mana_t har sus_unsus STATE1 576 1.5 1001 0.000359 -0.000294 779.625 0.0 Alabama 577 1.5 1003 0.000000 -0.004695 6358.433 0.0 Alabama 578 1.5 1005 0.000426 -0.000015 526.500 0.0 Alabama 850 1.5 1009 0.000865 0.000000 1107.736 0.0 Alabama 137 1.5 1011 0.000703 -0.000027 870.750 0.0 Alabama .. ... ... ... ... ... ... ... 789 1.5 51183 0.008107 -0.000483 10258.070 0.0 Virginia 387 1.5 51193 0.005570 -0.000395 7122.953 0.0 Virginia 388 1.5 51550 0.007986 -0.000220 9798.216 0.0 Virginia 389 1.5 51800 0.006605 -0.000068 7968.552 0.0 Virginia 390 1.5 51810 0.004112 -0.000053 4972.955 0.0 Virginia [852 rows x 7 columns]
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/3644696719.py:18: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. data['sus_unsus'].fillna(0, inplace=True)
# Filter rows where 'scenario' is equal to 1.5
state_sum1_15 = data15[data15['sus_unsus'] == 1]
# Define the columns you want to sum
columns_to_sum = ['irri_t','irri_mana_t']
state_sum1_15['irri_t'] = state_sum1_15['irri_t'].fillna(0)
state_sum1_15['irri_mana_t'] = state_sum1_15['irri_mana_t'].fillna(0)
# Group by 'STATE1' and calculate the sum for all specified columns at once
state_sum1_15 = state_sum1_15.groupby('STATE1')[columns_to_sum].sum().reset_index()
state_sum1_15 = state_sum1_15[['STATE1','irri_t','irri_mana_t']]
state_sum1_15 = state_sum1_15.merge(obs_t, on='STATE1', how='left')
state_sum1_15['unsus_gap_p']=state_sum1_15['irri_t']/state_sum1_15['obs_t']*100
state_sum1_15['unsus_gap_mana_p']=state_sum1_15['irri_mana_t']/state_sum1_15['obs_t']*100
state_sum1_15.rename(columns={'irri_t':'unsus_irri_t'}, inplace=True)
state_sum1_15.rename(columns={'irri_mana_t':'unsus_irri_mana_t'}, inplace=True)
state_sum1_15 = state_sum1_15.drop(columns='obs_t')
print(state_sum1_15)
state_sum0_15 = data15[data15['sus_unsus'] == 0]
# Define the columns you want to sum
columns_to_sum = ['irri_t','irri_mana_t']
state_sum0_15['irri_t'] = state_sum0_15['irri_t'].fillna(0)
state_sum0_15['irri_mana_t'] = state_sum0_15['irri_mana_t'].fillna(0)
# Group by 'STATE1' and calculate the sum for all specified columns at once
state_sum0_15 = state_sum0_15.groupby('STATE1')[columns_to_sum].sum().reset_index()
state_sum0_15 = state_sum0_15[['STATE1','irri_t','irri_mana_t']]
state_sum0_15 = state_sum0_15.merge(obs_t, on='STATE1', how='left')
state_sum0_15['sus_gap_p']=state_sum0_15['irri_t']/state_sum0_15['obs_t']*100
state_sum0_15['sus_gap_mana_p']=state_sum0_15['irri_mana_t']/state_sum0_15['obs_t']*100
state_sum0_15.rename(columns={'irri_t':'sus_irri_t'}, inplace=True)
state_sum0_15.rename(columns={'irri_mana_t':'sus_irri_mana_t'}, inplace=True)
state_sum0_15 = state_sum0_15.drop(columns='obs_t')
print(state_sum0_15)
state_sum15 = state_sum0_15.merge(state_sum1_15, on='STATE1', how='left')
state_sum15['irri_t']=state_sum15['sus_irri_t']+state_sum15['unsus_irri_t']
state_sum15 = state_sum15.merge(obs_t, on='STATE1', how='left')
state_sum15 = state_sum15.sort_values(by='obs_t', ascending=False)
state_sum15 = state_sum15.merge(err15, left_on='STATE1', right_on='STATE1')
print(state_sum15)
STATE1 unsus_irri_t unsus_irri_mana_t unsus_gap_p \ 0 Alabama 0.000000 -1.640132e-04 0.000000e+00 1 Arkansas 0.003506 -2.651787e-01 2.685377e-01 2 Delaware 0.012581 -8.877831e-03 3.206517e+01 3 Georgia 0.000000 -1.796392e-03 0.000000e+00 4 Illinois 0.405829 -1.201486e-02 4.518170e+02 5 Indiana 0.014635 0.000000e+00 6.513822e+01 6 Iowa 0.172095 -1.263437e-03 1.052434e+03 7 Kansas 0.896971 -2.273625e-01 3.423839e+02 8 Kentucky 0.031628 -3.795422e-04 4.856751e+02 9 Louisiana 0.043135 0.000000e+00 5.488986e+08 10 Maryland 0.002856 -2.132378e-05 1.108702e+01 11 Minnesota 0.069550 0.000000e+00 1.694531e+02 12 Mississippi 0.120640 0.000000e+00 1.195978e+07 13 Missouri 0.525107 2.954980e-07 4.795449e+06 14 Nebraska 0.172378 1.073797e-06 4.536726e+06 15 North Carolina 0.044491 -1.472312e-03 5.124197e+01 16 Oklahoma 0.045278 0.000000e+00 inf 17 South Carolina 0.000551 -1.380627e-06 2.080708e+00 18 Tennessee 0.077830 -3.143590e-03 2.069987e+02 19 Texas 0.031622 0.000000e+00 inf 20 Virginia 0.013922 -4.208697e-04 9.114176e+01 unsus_gap_mana_p 0 -0.613597 1 -20.312419 2 -22.626940 3 -4.226071 4 -13.376365 5 0.000000 6 -7.726469 7 -86.786803 8 -5.828260 9 0.000000 10 -0.082781 11 0.000000 12 0.000000 13 2.698582 14 28.260653 15 -1.695715 16 NaN 17 -0.005217 18 -8.360729 19 NaN 20 -2.755235 STATE1 sus_irri_t sus_irri_mana_t sus_gap_p sus_gap_mana_p 0 Alabama 0.092846 -2.547089e-02 3.473486e+02 -95.290298 1 Arkansas 0.077609 -1.039735e+00 5.944739e+00 -79.642657 2 Delaware 0.007412 -2.717736e-03 1.888997e+01 -6.926697 3 Florida 0.002815 0.000000e+00 1.386202e+03 0.000000 4 Georgia 0.009151 -1.052710e-02 2.152856e+01 -24.765330 5 Illinois 1.042322 2.982931e-01 1.160436e+03 332.095216 6 Indiana 0.091196 3.400853e-02 4.058968e+02 151.366588 7 Iowa 0.296205 5.675396e-02 1.811424e+03 347.075152 8 Kansas 0.266140 -3.420004e-02 1.015887e+02 -13.054539 9 Kentucky 0.212174 -2.876631e-03 3.258149e+03 -44.173628 10 Louisiana 0.335155 0.000000e+00 4.264848e+09 0.000000 11 Maryland 0.094998 -2.515376e-02 3.687935e+02 -97.649455 12 Minnesota 0.040894 -4.213393e-04 9.963629e+01 -1.026561 13 Mississippi 0.563163 -6.764142e-07 5.582976e+07 -67.057023 14 Missouri 1.269126 6.331057e-02 1.159006e+07 578172.348473 15 Nebraska 0.195429 6.256035e-07 5.143389e+06 16.464904 16 New Jersey 0.013556 0.000000e+00 1.818986e+07 0.000000 17 North Carolina 0.253798 -6.576783e-02 2.923081e+02 -75.747176 18 Oklahoma 0.100443 0.000000e+00 inf NaN 19 South Carolina 0.001330 -2.507932e-03 5.025051e+00 -9.476163 20 Tennessee 0.389767 -1.765717e-02 1.036628e+03 -46.961224 21 Texas 0.011168 0.000000e+00 inf NaN 22 Virginia 0.147665 -1.449605e-02 9.666916e+02 -94.898834 STATE1 sus_irri_t sus_irri_mana_t sus_gap_p sus_gap_mana_p \ 0 Arkansas 0.077609 -1.039735e+00 5.944739e+00 -79.642657 1 Kansas 0.266140 -3.420004e-02 1.015887e+02 -13.054539 2 Illinois 1.042322 2.982931e-01 1.160436e+03 332.095216 3 North Carolina 0.253798 -6.576783e-02 2.923081e+02 -75.747176 4 Georgia 0.009151 -1.052710e-02 2.152856e+01 -24.765330 5 Minnesota 0.040894 -4.213393e-04 9.963629e+01 -1.026561 6 Delaware 0.007412 -2.717736e-03 1.888997e+01 -6.926697 7 Tennessee 0.389767 -1.765717e-02 1.036628e+03 -46.961224 8 Alabama 0.092846 -2.547089e-02 3.473486e+02 -95.290298 9 South Carolina 0.001330 -2.507932e-03 5.025051e+00 -9.476163 10 Maryland 0.094998 -2.515376e-02 3.687935e+02 -97.649455 11 Indiana 0.091196 3.400853e-02 4.058968e+02 151.366588 12 Iowa 0.296205 5.675396e-02 1.811424e+03 347.075152 13 Virginia 0.147665 -1.449605e-02 9.666916e+02 -94.898834 14 Kentucky 0.212174 -2.876631e-03 3.258149e+03 -44.173628 15 Florida 0.002815 0.000000e+00 1.386202e+03 0.000000 16 Missouri 1.269126 6.331057e-02 1.159006e+07 578172.348473 17 Nebraska 0.195429 6.256035e-07 5.143389e+06 16.464904 18 Mississippi 0.563163 -6.764142e-07 5.582976e+07 -67.057023 19 New Jersey 0.013556 0.000000e+00 1.818986e+07 0.000000 20 Louisiana 0.335155 0.000000e+00 4.264848e+09 0.000000 21 Oklahoma 0.100443 0.000000e+00 inf NaN 22 Texas 0.011168 0.000000e+00 inf NaN unsus_irri_t unsus_irri_mana_t unsus_gap_p unsus_gap_mana_p irri_t \ 0 0.003506 -2.651787e-01 2.685377e-01 -20.312419 0.081114 1 0.896971 -2.273625e-01 3.423839e+02 -86.786803 1.163111 2 0.405829 -1.201486e-02 4.518170e+02 -13.376365 1.448151 3 0.044491 -1.472312e-03 5.124197e+01 -1.695715 0.298289 4 0.000000 -1.796392e-03 0.000000e+00 -4.226071 0.009151 5 0.069550 0.000000e+00 1.694531e+02 0.000000 0.110444 6 0.012581 -8.877831e-03 3.206517e+01 -22.626940 0.019993 7 0.077830 -3.143590e-03 2.069987e+02 -8.360729 0.467597 8 0.000000 -1.640132e-04 0.000000e+00 -0.613597 0.092846 9 0.000551 -1.380627e-06 2.080708e+00 -0.005217 0.001881 10 0.002856 -2.132378e-05 1.108702e+01 -0.082781 0.097854 11 0.014635 0.000000e+00 6.513822e+01 0.000000 0.105831 12 0.172095 -1.263437e-03 1.052434e+03 -7.726469 0.468300 13 0.013922 -4.208697e-04 9.114176e+01 -2.755235 0.161587 14 0.031628 -3.795422e-04 4.856751e+02 -5.828260 0.243802 15 NaN NaN NaN NaN NaN 16 0.525107 2.954980e-07 4.795449e+06 2.698582 1.794233 17 0.172378 1.073797e-06 4.536726e+06 28.260653 0.367807 18 0.120640 0.000000e+00 1.195978e+07 0.000000 0.683803 19 NaN NaN NaN NaN NaN 20 0.043135 0.000000e+00 5.488986e+08 0.000000 0.378290 21 0.045278 0.000000e+00 inf NaN 0.145721 22 0.031622 0.000000e+00 inf NaN 0.042789 obs_t p95_15 p5_15 p5_mana_15 p95_mana_15 0 1.305501e+00 7.859204e+01 4.804079e-01 0.000000 0.374630 1 2.619782e-01 5.413222e+02 8.035247e+00 0.000000 0.000000 2 8.982157e-02 1.449517e+03 5.535976e+02 81.001034 89.580134 3 8.682545e-02 4.091946e+02 2.664222e+01 1.117400 3.427073 4 4.250739e-02 5.417544e+01 2.014772e+00 4.123399 3.597527 5 4.104377e-02 2.175321e+02 8.196079e+01 0.000000 1.026561 6 3.923567e-02 9.707707e+01 1.888997e+01 12.255505 5.031586 7 3.759947e-02 1.332756e+03 7.544312e+01 29.966272 99.300143 8 2.672979e-02 4.290232e+02 8.813377e+00 3.286652 27.001409 9 2.646569e-02 1.658714e+01 0.000000e+00 0.000000 0.124535 10 2.575924e-02 4.776128e+02 2.295084e+01 0.000000 0.148200 11 2.246766e-02 4.670944e+02 2.833211e+01 118.865112 38.608740 12 1.635207e-02 2.704453e+03 3.899751e+02 124.221942 159.264931 13 1.527527e-02 1.039205e+03 3.687244e+01 0.000000 2.115864 14 6.512101e-03 3.793823e+03 1.157950e+02 0.000000 0.000000 15 2.030791e-04 1.386199e+03 0.000000e+00 0.000000 0.000000 16 1.095012e-05 1.608879e+07 4.756684e+05 133855.276260 310644.397057 17 3.799619e-06 9.593469e+06 1.563826e+06 0.000000 0.000000 18 1.008715e-06 6.778961e+07 0.000000e+00 0.000000 0.000000 19 7.452582e-08 1.818986e+07 1.336915e+07 0.000000 0.000000 20 7.858539e-09 4.813747e+09 0.000000e+00 0.000000 0.000000 21 0.000000e+00 NaN NaN NaN NaN 22 0.000000e+00 NaN NaN NaN NaN
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/1885324761.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 state_sum1_15['irri_t'] = state_sum1_15['irri_t'].fillna(0) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/1885324761.py:6: 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 state_sum1_15['irri_mana_t'] = state_sum1_15['irri_mana_t'].fillna(0) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/1885324761.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 state_sum0_15['irri_t'] = state_sum0_15['irri_t'].fillna(0) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/1885324761.py:22: 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 state_sum0_15['irri_mana_t'] = state_sum0_15['irri_mana_t'].fillna(0)
# Calculate additional attributes
state_sum15['irri_mana_t'] = state_sum15['sus_irri_mana_t'] + state_sum15['unsus_irri_mana_t']
obs_t = state_sum15['obs_t'].sum()
sus_irri = state_sum15['sus_irri_t'].sum()
unsus_irri = state_sum15['unsus_irri_t'].sum()
sus_irri_mana = state_sum15['sus_irri_mana_t'].sum()
unsus_irri_mana = state_sum15['unsus_irri_mana_t'].sum()
print(sus_irri,unsus_irri,sus_irri_mana, unsus_irri_mana)
sus_irri_p=sus_irri/obs_t*100
unsus_irri_p=unsus_irri/obs_t*100
sus_irri_mana_p=sus_irri_mana/obs_t*100
unsus_irri_mana_p=unsus_irri_mana/obs_t*100
df_sum = pd.DataFrame({
'sus_irri_p': [sus_irri_p],
'unsus_irri_p': [unsus_irri_p],
'sus_irri_mana_p': [sus_irri_mana_p],
'unsus_irri_mana_p': [unsus_irri_mana_p]
})
print(df_sum)
df_sum['STATE1']='U.S.'
df_sum['t']=df_sum['sus_irri_p']+df_sum['unsus_irri_p']
df_sum['t_mana']=df_sum['sus_irri_mana_p']+df_sum['unsus_irri_mana_p']
df_sum=df_sum.merge(err_country15, left_on='STATE1', right_on='STATE1')
print(df_sum)
# Define the columns you want to plot
columns_to_plot = ['sus_irri_mana_p','unsus_irri_mana_p', 'sus_irri_p','unsus_irri_p']
columns_to_plot1 = ['t_mana', 'p5_mana_15','p95_mana_15','t','p5_15','p95_15']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['#fff59d', '#fff59d', '#b2ebf2', '#b2ebf2'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(df_sum['STATE1']))
# Create a figure and axis
fig, ax = plt.subplots(figsize=(5, 1)) # Adjust the figure size as needed
# Initialize left positions for each group
left_group1 = np.zeros(len(df_sum['STATE1']))
left_group2 = np.zeros(len(df_sum['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
if 'unsus_irri_mana_p' in columns_to_plot[i]:
hatch_pattern = '//'
elif 'unsus_irri_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, df_sum[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1, capsize=3, capstyle='round')
left_group1 += df_sum[columns_to_plot[i]]
main_data = df_sum[columns_to_plot1[0]]
error_lower = (df_sum[columns_to_plot1[ 1]]).values
error_upper = (df_sum[columns_to_plot1[ 2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, df_sum[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1, capsize=3, capstyle='round')
left_group2 += df_sum[columns_to_plot[i]]
main_data = df_sum[columns_to_plot1[3]]
error_lower = (df_sum[columns_to_plot1[4]]).values
error_upper = (df_sum[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
# Set y-tick labels
plt.yticks([0], df_sum['STATE1'], fontfamily='Arial')
# Set labels and title
ax.set_yticks(index)
ax.set_yticklabels(df_sum['STATE1'], fontfamily='Arial', fontsize=20)
ax.set_xticks(np.arange(-100, 900, 200))
ax.axvline(0, color='black', linestyle='--', linewidth=1)
ax.tick_params(axis='x', labelsize=20)
# Save the entire figure as a JPG with DPI 300
plt.savefig('national_soybean_irri_15.png', format='png', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
5.514359917403042 2.6846049953937583 -0.789165656222785 -0.5220953863398782 sus_irri_p unsus_irri_p sus_irri_mana_p unsus_irri_mana_p 0 269.744143 131.321946 -38.603359 -25.53917 sus_irri_p unsus_irri_p sus_irri_mana_p unsus_irri_mana_p STATE1 \ 0 269.744143 131.321946 -38.603359 -25.53917 U.S. t t_mana p95_15 p5_15 p95_mana_15 p5_mana_15 0 401.066089 -64.142528 41.683722 31.921113 4.534483 4.328059
state_sum15_10=state_sum15.nlargest(10, 'obs_t')
state_sum15_10=state_sum15_10[['sus_gap_p', 'sus_gap_mana_p', 'unsus_gap_p', 'unsus_gap_mana_p',
'p5_mana_15','p95_mana_15','p5_15','p95_15','STATE1']]
state_sum15_10['unsus_gap_p'] = state_sum15_10['unsus_gap_p'].fillna(0)
state_sum15_10['unsus_gap_mana_p'] = state_sum15_10['unsus_gap_mana_p'].fillna(0)
state_sum15_10['gap_p']=state_sum15_10['sus_gap_p']+state_sum15_10['unsus_gap_p']
state_sum15_10['gap_mana_p']=state_sum15_10['sus_gap_mana_p']+state_sum15_10['unsus_gap_mana_p']
print(state_sum15_10)
state_sum15_10 = state_sum15_10.sort_values(by='gap_p', ascending=False)
# Define the columns you want to plot
columns_to_plot = ['sus_gap_mana_p', 'unsus_gap_mana_p', 'sus_gap_p', 'unsus_gap_p']
columns_to_plot1 = ['gap_mana_p', 'p5_mana_15','p95_mana_15','gap_p','p5_15','p95_15']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['#fff59d', '#fff59d', '#b2ebf2', '#b2ebf2'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(state_sum15_10['STATE1']))
# Expand the length of the y-axis (adjust the figure size)
plt.figure(figsize=(5, 12))
# Initialize left positions for each group
left_group1 = np.zeros(len(state_sum15_10['STATE1']))
left_group2 = np.zeros(len(state_sum15_10['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
std_err_col = None # Initialize std_err_col
if 'unsus_gap_mana_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add hatch for 'unsus_irri_mana_t' columns
elif 'unsus_gap_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, state_sum15_10[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1,capsize=3, capstyle='round')
left_group1 += state_sum15_10[columns_to_plot[i]]
main_data = state_sum15_10[columns_to_plot1[0]]
error_lower = (state_sum15_10[columns_to_plot1[1]]).values
error_upper = (state_sum15_10[columns_to_plot1[2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, state_sum15_10[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1,capsize=3, capstyle='round')
left_group2 += state_sum15_10[columns_to_plot[i]]
main_data = state_sum15_10[columns_to_plot1[3]]
error_lower = (state_sum15_10[columns_to_plot1[4]]).values
error_upper = (state_sum15_10[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[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 / 2, state_sum15_10['STATE1'], fontfamily='Arial')
# Set the x-axis label
plt.xlabel('Irrigation withdrawals (%)', fontfamily='Arial', fontsize=22)
# Set the x-ticks
x_ticks = np.arange(-200, 6500, 1600)
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
plt.text(0.85, 0.8, '(c) Soybean under\n1.5°C warming', 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=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('state_soybean_irri_15_10.png', format='png', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
sus_gap_p sus_gap_mana_p unsus_gap_p unsus_gap_mana_p p5_mana_15 \ 0 5.944739 -79.642657 0.268538 -20.312419 0.000000 1 101.588684 -13.054539 342.383904 -86.786803 0.000000 2 1160.435698 332.095216 451.817003 -13.376365 81.001034 3 292.308069 -75.747176 51.241971 -1.695715 1.117400 4 21.528558 -24.765330 0.000000 -4.226071 4.123399 5 99.636291 -1.026561 169.453134 0.000000 0.000000 6 18.889968 -6.926697 32.065171 -22.626940 12.255505 7 1036.627707 -46.961224 206.998746 -8.360729 29.966272 8 347.348570 -95.290298 0.000000 -0.613597 3.286652 9 5.025051 -9.476163 2.080708 -0.005217 0.000000 p95_mana_15 p5_15 p95_15 STATE1 gap_p \ 0 0.374630 0.480408 78.592037 Arkansas 6.213277 1 0.000000 8.035247 541.322234 Kansas 443.972587 2 89.580134 553.597607 1449.516712 Illinois 1612.252701 3 3.427073 26.642222 409.194567 North Carolina 343.550041 4 3.597527 2.014772 54.175437 Georgia 21.528558 5 1.026561 81.960793 217.532066 Minnesota 269.089424 6 5.031586 18.889968 97.077069 Delaware 50.955139 7 99.300143 75.443117 1332.756488 Tennessee 1243.626453 8 27.001409 8.813377 429.023167 Alabama 347.348570 9 0.124535 0.000000 16.587138 South Carolina 7.105758 gap_mana_p 0 -99.955076 1 -99.841342 2 318.718851 3 -77.442891 4 -28.991401 5 -1.026561 6 -29.553637 7 -55.321953 8 -95.903895 9 -9.481380
state_sum15_10_1 = state_sum15_10.tail(6)
# Define the columns you want to plot
columns_to_plot = ['sus_gap_mana_p', 'unsus_gap_mana_p', 'sus_gap_p', 'unsus_gap_p']
columns_to_plot1 = ['gap_mana_p', 'p5_mana_15','p95_mana_15','gap_p','p5_15','p95_15']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['#fff59d', '#fff59d', '#b2ebf2', '#b2ebf2'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(state_sum15_10_1['STATE1']))
# Expand the length of the y-axis (adjust the figure size)
plt.figure(figsize=(3, 6), facecolor='none')
# Initialize left positions for each group
left_group1 = np.zeros(len(state_sum15_10_1['STATE1']))
left_group2 = np.zeros(len(state_sum15_10_1['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
std_err_col = None # Initialize std_err_col
if 'unsus_gap_mana_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add hatch for 'unsus_irri_mana_t' columns
elif 'unsus_gap_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, state_sum15_10_1[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1,capsize=3, capstyle='round')
left_group1 += state_sum15_10_1[columns_to_plot[i]]
main_data = state_sum15_10_1[columns_to_plot1[0]]
error_lower = (state_sum15_10_1[columns_to_plot1[1]]).values
error_upper = (state_sum15_10_1[columns_to_plot1[2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, state_sum15_10_1[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1,capsize=3, capstyle='round')
left_group2 += state_sum15_10_1[columns_to_plot[i]]
main_data = state_sum15_10_1[columns_to_plot1[3]]
error_lower = (state_sum15_10_1[columns_to_plot1[4]]).values
error_upper = (state_sum15_10_1[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[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 / 2, state_sum15_10_1['STATE1'], fontfamily='Arial')
# Set the x-ticks
x_ticks = np.arange(-200, 1400, 500)
plt.xticks(x_ticks, fontfamily='Arial')
# Add a dashed line at x=0
ax = plt.gca()
ax.axvline(0, color='black', linestyle='--', linewidth=1)
# Set tick label font sizes
ax.tick_params(axis='both', labelsize=16)
ax.set_xticklabels(ax.get_xticklabels(), fontfamily='Arial')
ax.set_yticklabels(ax.get_yticklabels(), fontfamily='Arial')
ax.spines['bottom'].set_linewidth(1.5) # Set x-axis line width
ax.spines['left'].set_linewidth(1.5) # Set y-axis line width
ax.spines['right'].set_linewidth(1.5) # Set right axis line width
ax.spines['top'].set_linewidth(1.5) # Set top axis line width
# Save the entire figure as a JPG with DPI 300
plt.savefig('state_soybean_irri_15_10_tail.png', format='png', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
# sustain and unsustain under 3 warming scenairo
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Figure 3 Yiled under warming scenario/off_soybean_mana_lnfer_irri.csv"
data1 = pd.read_csv(excel_file)
data1 = data1[data1['scenario'] == 3]
data1.rename(columns={'geoid': 'GEOID'}, inplace=True)
data1['GEOID'] = data1['GEOID'].astype(int)
print(data1)
# Convert 'GEOID' column to int64 in gazetteer_data
gazetteer_data['GEOID'] = gazetteer_data['GEOID'].astype(int)
mean_sus_unsus3['GEOID'] = mean_sus_unsus3['GEOID'].astype(int)
data=data1.merge(gazetteer_data, on='GEOID', how='left')
data=data.merge(mean_sus_unsus3, on='GEOID', how='left')
data['irrigation_t']=(data['irri_need_1']-data['irrigation1'])*data['har']/1000000 #km3
data['irrigation_mana_t']=data['gap_irrigation']*data['har']/1000000 #km3
# Replace NaN values in 'sus_unsus' column with 0
data['sus_unsus'].fillna(0, inplace=True)
data.rename(columns={'GEOID':'geoid'}, inplace=True)
data = data.sort_values(by='geoid')
print(data)
data.rename(columns={'irrigation_t':'irri_t'}, inplace=True)
data.rename(columns={'irrigation_mana_t':'irri_mana_t'}, inplace=True)
data3 = data[['scenario','geoid','irri_t','irri_mana_t','har','sus_unsus','STATE1']]
print(data3)
tmp pre lnfer irrigation1 y_temp mana_group GEOID \ 0 22.760628 5.757780 2.038479 0.001818 7.979504 1 39017 1 27.027358 6.635976 2.035268 0.126456 7.761395 1 13043 2 25.319158 7.937325 1.236794 0.000507 7.905103 1 37177 3 24.492824 5.942823 0.295453 0.000797 7.711970 1 17065 5 25.244316 6.669287 2.480696 0.042775 8.064906 1 37045 ... ... ... ... ... ... ... ... 1993 25.723312 6.718675 4.369129 0.149384 8.278050 3 1043 1994 26.457528 6.493699 2.301965 0.027715 7.966898 3 1075 1995 27.248052 6.264505 3.149844 0.318468 7.910322 3 1085 1996 26.679214 6.355030 4.096728 0.210819 8.090918 3 1087 1997 27.222396 6.256731 2.847938 0.009984 7.908306 3 1091 scenario lnyield_obs scenario.1 har gap_fer \ 0 3.0 7.994525 3.0 14757.7400 1.295049 1 3.0 7.499042 3.0 1336.5000 24.104553 2 3.0 7.918210 3.0 12818.7600 8.163098 3 3.0 7.946740 3.0 37450.0400 1.483279 5 3.0 7.648014 3.0 5522.7150 35.724095 ... ... ... ... ... ... 1993 3.0 7.978103 3.0 2906.2460 364.946897 1994 3.0 7.625159 3.0 564.0975 71.562105 1995 3.0 7.784370 3.0 1699.6330 184.540509 1996 3.0 7.847030 3.0 598.4888 298.745838 1997 3.0 7.563227 3.0 1578.0490 121.360035 gap_irrigation lnfer_need lnfer_values irri_need y_temp_base2015 \ 0 -0.001794 2.194755 2.194328 0.837454 7.979504 1 -0.126432 3.559168 3.458172 0.837454 7.761395 2 -0.000484 2.452045 2.451664 0.837454 7.905103 3 -0.000773 1.039118 1.039221 NaN 7.711970 5 -0.042751 3.908331 3.864379 0.837454 8.064906 ... ... ... ... ... ... 1993 0.000000 6.095648 6.095648 0.837454 8.278050 1994 0.000000 4.401289 4.401289 0.837454 7.966898 1995 0.000000 5.336927 5.336927 0.837454 7.910322 1996 0.000000 5.883013 5.883013 0.837454 8.090918 1997 0.000000 4.931680 4.931680 0.837454 7.908306 y_temp_base_t irri_need_1 y_temp_irri 0 7.958094 0.837454 7.938183 1 7.561807 0.837454 7.540154 2 7.738559 0.837454 7.721508 3 7.610054 0.837454 7.647000 5 7.872395 0.837454 7.826035 ... ... ... ... 1993 8.054614 0.837454 7.957525 1994 7.681917 0.837454 7.649035 1995 7.639415 0.837454 7.608856 1996 7.862778 0.837454 7.798237 1997 7.623575 0.837454 7.580939 [1146 rows x 20 columns] tmp pre lnfer irrigation1 y_temp mana_group geoid \ 1137 26.784940 6.208599 2.457656 0.377088 7.871762 3 1001 835 27.808318 8.926058 1.292553 0.738441 7.601908 2 1003 836 27.067738 6.742623 2.114730 0.028216 7.821927 2 1005 1138 25.706190 6.658286 4.361742 0.056342 8.285447 3 1009 216 26.923034 6.491163 4.858469 0.030586 8.201724 1 1011 ... ... ... ... ... ... ... ... 598 24.587320 6.583579 1.287505 0.055504 7.938844 1 51193 599 25.147098 7.570098 1.553341 0.022453 7.970973 1 51550 600 25.061998 7.586472 1.187985 0.008524 7.928655 1 51800 601 24.859328 7.207139 2.482080 0.010662 8.111622 1 51810 602 23.294406 5.721528 0.935788 0.000171 7.820683 1 54037 scenario lnyield_obs scenario.1 ... y_temp_base_t irri_need_1 \ 1137 3.0 7.509678 3.0 ... 7.622792 0.837454 835 3.0 7.927428 3.0 ... 7.351790 0.837454 836 3.0 8.161582 3.0 ... 7.574791 0.837454 1138 3.0 7.840701 3.0 ... 8.067909 0.837454 216 3.0 7.984614 3.0 ... 7.957275 0.837454 ... ... ... ... ... ... ... 598 3.0 7.805104 3.0 ... 7.776233 0.837454 599 3.0 7.885281 3.0 ... 7.792875 0.837454 600 3.0 7.798817 3.0 ... 7.753334 0.837454 601 3.0 7.872881 3.0 ... 7.939772 0.837454 602 3.0 7.900531 3.0 ... 7.775021 0.837454 y_temp_irri USPS Region STATE1 stateansi sus_unsus \ 1137 7.606994 AL E SOUTH CENTRAL Alabama 1.0 0.0 835 7.348977 AL E SOUTH CENTRAL Alabama 1.0 0.0 836 7.544787 AL E SOUTH CENTRAL Alabama 1.0 0.0 1138 7.958548 AL E SOUTH CENTRAL Alabama 1.0 0.0 216 7.841922 AL E SOUTH CENTRAL Alabama 1.0 0.0 ... ... ... ... ... ... ... 598 7.773208 VA SOUTHEAST Virginia 51.0 0.0 599 7.765774 VA SOUTHEAST Virginia 51.0 0.0 600 7.742675 VA SOUTHEAST Virginia 51.0 0.0 601 7.876415 VA SOUTHEAST Virginia 51.0 0.0 602 7.792047 WV NORTHEAST West Virginia 54.0 0.0 irrigation_t irrigation_mana_t 1137 0.000359 0.000000e+00 835 0.000630 -4.695179e-03 836 0.000426 -1.484315e-05 1138 0.000865 0.000000e+00 216 0.000703 -2.661240e-05 ... ... ... 598 0.005570 -3.951858e-04 599 0.007986 -2.197624e-04 600 0.006605 -6.773285e-05 601 0.004112 -5.290140e-05 602 0.003330 -5.866308e-07 [1146 rows x 27 columns] scenario geoid irri_t irri_mana_t har sus_unsus \ 1137 3.0 1001 0.000359 0.000000e+00 779.625 0.0 835 3.0 1003 0.000630 -4.695179e-03 6358.433 0.0 836 3.0 1005 0.000426 -1.484315e-05 526.500 0.0 1138 3.0 1009 0.000865 0.000000e+00 1107.736 0.0 216 3.0 1011 0.000703 -2.661240e-05 870.750 0.0 ... ... ... ... ... ... ... 598 3.0 51193 0.005570 -3.951858e-04 7122.953 0.0 599 3.0 51550 0.007986 -2.197624e-04 9798.216 0.0 600 3.0 51800 0.006605 -6.773285e-05 7968.552 0.0 601 3.0 51810 0.004112 -5.290140e-05 4972.955 0.0 602 3.0 54037 0.003330 -5.866308e-07 3976.619 0.0 STATE1 1137 Alabama 835 Alabama 836 Alabama 1138 Alabama 216 Alabama ... ... 598 Virginia 599 Virginia 600 Virginia 601 Virginia 602 West Virginia [1146 rows x 7 columns]
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/2106963079.py:18: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. data['sus_unsus'].fillna(0, inplace=True)
# Load data from an Excel file
excel_file = "/Users/chenchenren/postdoc/paper/2N and water-US/Figure 4 Pattern/pattern_base.xlsx"
sheet_name = "soybean"
data1 = pd.read_excel(excel_file, sheet_name=sheet_name)
data1['obs_t']=data1['irrigation1']*data1['har']/1000000 #km3
print(data1)
data1['geoid'] = data1['geoid'].astype(float)
# Rename the 'geoid' column to 'GEOID' in year_2020_data
data1.rename(columns={'geoid': 'GEOID'}, inplace=True)
obs = data1.merge(gazetteer_data, on='GEOID', how='left')
columns_to_sum = ['obs_t']
obs_t = obs.groupby('STATE1')[columns_to_sum].sum().reset_index()
print(obs_t)
geoid irrigation1 har yield production fer obs_t 0 1001 0.377088 779.625 1.825626 0.142330 0.011677 0.000294 1 1003 0.738441 6358.433 2.772287 1.762740 0.003642 0.004695 2 1005 0.028216 526.500 3.503725 0.184471 0.008287 0.000015 3 1009 0.056342 1107.736 2.541985 0.281585 0.078394 0.000062 4 1011 0.030586 870.750 2.935445 0.255604 0.128827 0.000027 ... ... ... ... ... ... ... ... 1768 55133 0.000000 7837.872 2.907065 2.278520 0.004555 0.000000 1769 55135 0.000000 9909.051 2.857244 2.831258 0.004333 0.000000 1770 55137 0.000000 5335.875 2.810866 1.499843 0.004727 0.000000 1771 55139 0.000000 14541.420 3.068204 4.461603 0.003508 0.000000 1772 55141 0.000000 6700.507 2.754878 1.845908 0.012887 0.000000 [1773 rows x 7 columns] STATE1 obs_t 0 Alabama 2.672979e-02 1 Arkansas 1.305501e+00 2 Delaware 3.923567e-02 3 Florida 2.030791e-04 4 Georgia 4.250739e-02 5 Illinois 8.982157e-02 6 Indiana 2.246766e-02 7 Iowa 1.635207e-02 8 Kansas 2.619782e-01 9 Kentucky 6.512101e-03 10 Louisiana 7.858539e-09 11 Maryland 2.575924e-02 12 Michigan 5.041988e-02 13 Minnesota 4.104377e-02 14 Mississippi 1.008715e-06 15 Missouri 1.095012e-05 16 Nebraska 3.799619e-06 17 New Jersey 7.452582e-08 18 New York 6.661501e-04 19 North Carolina 8.682545e-02 20 North Dakota 0.000000e+00 21 Ohio 6.057680e-03 22 Oklahoma 0.000000e+00 23 Pennsylvania 6.010994e-04 24 South Carolina 2.646569e-02 25 South Dakota 4.024230e-02 26 Tennessee 3.759947e-02 27 Texas 0.000000e+00 28 Virginia 1.527527e-02 29 West Virginia 6.815925e-07 30 Wisconsin 0.000000e+00
# Filter rows where 'scenario' is equal to 3
state_sum1_3 = data3[data3['sus_unsus'] == 1]
# Define the columns you want to sum
columns_to_sum = ['irri_t','irri_mana_t']
state_sum1_3['irri_t'] = state_sum1_3['irri_t'].fillna(0)
state_sum1_3['irri_mana_t'] = state_sum1_3['irri_mana_t'].fillna(0)
# Group by 'STATE1' and calculate the sum for all specified columns at once
state_sum1_3 = state_sum1_3.groupby('STATE1')[columns_to_sum].sum().reset_index()
state_sum1_3 = state_sum1_3[['STATE1','irri_t','irri_mana_t']]
state_sum1_3 = state_sum1_3.merge(obs_t, on='STATE1', how='left')
state_sum1_3['unsus_gap_p']=state_sum1_3['irri_t']/state_sum1_3['obs_t']*100
state_sum1_3['unsus_gap_mana_p']=state_sum1_3['irri_mana_t']/state_sum1_3['obs_t']*100
state_sum1_3.rename(columns={'irri_t':'unsus_irri_t'}, inplace=True)
state_sum1_3.rename(columns={'irri_mana_t':'unsus_irri_mana_t'}, inplace=True)
state_sum1_3 = state_sum1_3.drop(columns='obs_t')
print(state_sum1_3)
state_sum0_3 = data3[data3['sus_unsus'] == 0]
# Define the columns you want to sum
columns_to_sum = ['irri_t','irri_mana_t']
state_sum0_3['irri_t'] = state_sum0_3['irri_t'].fillna(0)
state_sum0_3['irri_mana_t'] = state_sum0_3['irri_mana_t'].fillna(0)
# Group by 'STATE1' and calculate the sum for all specified columns at once
state_sum0_3 = state_sum0_3.groupby('STATE1')[columns_to_sum].sum().reset_index()
state_sum0_3 = state_sum0_3[['STATE1','irri_t','irri_mana_t']]
state_sum0_3 = state_sum0_3.merge(obs_t, on='STATE1', how='left')
state_sum0_3['sus_gap_p']=state_sum0_3['irri_t']/state_sum0_3['obs_t']*100
state_sum0_3['sus_gap_mana_p']=state_sum0_3['irri_mana_t']/state_sum0_3['obs_t']*100
state_sum0_3.rename(columns={'irri_t':'sus_irri_t'}, inplace=True)
state_sum0_3.rename(columns={'irri_mana_t':'sus_irri_mana_t'}, inplace=True)
state_sum0_3 = state_sum0_3.drop(columns='obs_t')
print(state_sum0_3)
state_sum3 = state_sum0_3.merge(state_sum1_3, on='STATE1', how='left')
state_sum3['irri_t']=state_sum3['sus_irri_t']+state_sum3['unsus_irri_t']
state_sum3 = state_sum3.merge(obs_t, on='STATE1', how='left')
state_sum3 = state_sum3.sort_values(by='obs_t', ascending=False)
state_sum3 = state_sum3.merge(err3, left_on='STATE1', right_on='STATE1')
print(state_sum3)
STATE1 unsus_irri_t unsus_irri_mana_t unsus_gap_p \ 0 Alabama 0.001369 -0.000164 5.121938e+00 1 Arkansas 0.012315 -0.270238 9.433064e-01 2 Delaware 0.012581 -0.008878 3.206517e+01 3 Georgia 0.001815 -0.005357 4.269414e+00 4 Illinois 1.231407 -0.015525 1.370948e+03 5 Indiana 0.147097 -0.000275 6.547035e+02 6 Iowa 0.466358 -0.001595 2.851983e+03 7 Kansas 0.922968 -0.227739 3.523071e+02 8 Kentucky 0.115329 -0.000667 1.771001e+03 9 Louisiana 0.136553 0.000000 1.737642e+09 10 Maryland 0.007874 -0.000210 3.056769e+01 11 Mississippi 0.135161 0.000000 1.339936e+07 12 Missouri 0.944659 -0.000002 8.626931e+06 13 Nebraska 0.716954 0.000014 1.886910e+07 14 North Carolina 0.045434 -0.002142 5.232758e+01 15 Ohio 0.012332 -0.000026 2.035779e+02 16 Oklahoma 0.059149 0.000000 inf 17 Pennsylvania 0.005445 -0.000034 9.057808e+02 18 South Carolina 0.011596 -0.007249 4.381500e+01 19 Tennessee 0.182140 -0.013698 4.844223e+02 20 Texas 0.034271 0.000000 inf 21 Virginia 0.015492 -0.000549 1.014195e+02 unsus_gap_mana_p 0 -0.613597 1 -20.699935 2 -22.626940 3 -12.603192 4 -17.283819 5 -1.225652 6 -9.756365 7 -86.930621 8 -10.244621 9 0.000000 10 -0.817052 11 0.000000 12 -18.274646 13 355.909897 14 -2.467429 15 -0.437035 16 NaN 17 -5.598919 18 -27.391644 19 -36.430755 20 NaN 21 -3.591091 STATE1 sus_irri_t sus_irri_mana_t sus_gap_p sus_gap_mana_p 0 Alabama 0.099999 -2.237756e-02 3.741093e+02 -83.717695 1 Arkansas 0.068799 -6.272401e-01 5.269971e+00 -48.045948 2 Delaware 0.007412 -3.035620e-02 1.888997e+01 -77.368899 3 Florida 0.004647 -2.030441e-04 2.288288e+03 -99.982769 4 Georgia 0.041759 -3.320012e-02 9.824015e+01 -78.104355 5 Illinois 1.622755 1.736197e-01 1.806643e+03 193.294011 6 Indiana 0.988061 7.394282e-02 4.397702e+03 329.107835 7 Iowa 0.547611 2.038336e-02 3.348877e+03 124.653094 8 Kansas 0.266140 -3.420004e-02 1.015887e+02 -13.054539 9 Kentucky 0.434981 -5.796825e-03 6.679574e+03 -89.016208 10 Louisiana 0.241737 0.000000e+00 3.076104e+09 0.000000 11 Maryland 0.130503 -2.067254e-02 5.066259e+02 -80.252891 12 Mississippi 0.548642 -6.764142e-07 5.439018e+07 -67.057023 13 Missouri 0.867007 -8.909902e-07 7.917785e+06 -8.136808 14 Nebraska 0.504725 -3.507466e-07 1.328356e+07 -9.231100 15 New Jersey 0.017119 -7.004212e-09 2.297027e+07 -9.398370 16 North Carolina 0.414018 -7.354802e-02 4.768390e+02 -84.707903 17 Ohio 0.058243 8.012802e-03 9.614675e+02 132.275091 18 Oklahoma 0.086571 0.000000e+00 inf NaN 19 Pennsylvania 0.031418 4.676175e-03 5.226805e+03 777.936998 20 South Carolina 0.097786 -1.839174e-02 3.694828e+02 -69.492788 21 Tennessee 0.293499 -2.337716e-02 7.805946e+02 -62.174162 22 Texas 0.011168 0.000000e+00 inf NaN 23 Virginia 0.157424 -1.302344e-02 1.030582e+03 -85.258336 24 West Virginia 0.003330 -5.866308e-07 4.884962e+05 -86.067678 STATE1 sus_irri_t sus_irri_mana_t sus_gap_p sus_gap_mana_p \ 0 Arkansas 0.068799 -6.272401e-01 5.269971e+00 -48.045948 1 Kansas 0.266140 -3.420004e-02 1.015887e+02 -13.054539 2 Illinois 1.622755 1.736197e-01 1.806643e+03 193.294011 3 North Carolina 0.414018 -7.354802e-02 4.768390e+02 -84.707903 4 Georgia 0.041759 -3.320012e-02 9.824015e+01 -78.104355 5 Delaware 0.007412 -3.035620e-02 1.888997e+01 -77.368899 6 Tennessee 0.293499 -2.337716e-02 7.805946e+02 -62.174162 7 Alabama 0.099999 -2.237756e-02 3.741093e+02 -83.717695 8 South Carolina 0.097786 -1.839174e-02 3.694828e+02 -69.492788 9 Maryland 0.130503 -2.067254e-02 5.066259e+02 -80.252891 10 Indiana 0.988061 7.394282e-02 4.397702e+03 329.107835 11 Iowa 0.547611 2.038336e-02 3.348877e+03 124.653094 12 Virginia 0.157424 -1.302344e-02 1.030582e+03 -85.258336 13 Kentucky 0.434981 -5.796825e-03 6.679574e+03 -89.016208 14 Ohio 0.058243 8.012802e-03 9.614675e+02 132.275091 15 Pennsylvania 0.031418 4.676175e-03 5.226805e+03 777.936998 16 Florida 0.004647 -2.030441e-04 2.288288e+03 -99.982769 17 Missouri 0.867007 -8.909902e-07 7.917785e+06 -8.136808 18 Nebraska 0.504725 -3.507466e-07 1.328356e+07 -9.231100 19 Mississippi 0.548642 -6.764142e-07 5.439018e+07 -67.057023 20 West Virginia 0.003330 -5.866308e-07 4.884962e+05 -86.067678 21 New Jersey 0.017119 -7.004212e-09 2.297027e+07 -9.398370 22 Louisiana 0.241737 0.000000e+00 3.076104e+09 0.000000 23 Oklahoma 0.086571 0.000000e+00 inf NaN 24 Texas 0.011168 0.000000e+00 inf NaN unsus_irri_t unsus_irri_mana_t unsus_gap_p unsus_gap_mana_p irri_t \ 0 0.012315 -0.270238 9.433064e-01 -20.699935 0.081114 1 0.922968 -0.227739 3.523071e+02 -86.930621 1.189108 2 1.231407 -0.015525 1.370948e+03 -17.283819 2.854162 3 0.045434 -0.002142 5.232758e+01 -2.467429 0.459451 4 0.001815 -0.005357 4.269414e+00 -12.603192 0.043574 5 0.012581 -0.008878 3.206517e+01 -22.626940 0.019993 6 0.182140 -0.013698 4.844223e+02 -36.430755 0.475640 7 0.001369 -0.000164 5.121938e+00 -0.613597 0.101368 8 0.011596 -0.007249 4.381500e+01 -27.391644 0.109382 9 0.007874 -0.000210 3.056769e+01 -0.817052 0.138377 10 0.147097 -0.000275 6.547035e+02 -1.225652 1.135157 11 0.466358 -0.001595 2.851983e+03 -9.756365 1.013969 12 0.015492 -0.000549 1.014195e+02 -3.591091 0.172916 13 0.115329 -0.000667 1.771001e+03 -10.244621 0.550310 14 0.012332 -0.000026 2.035779e+02 -0.437035 0.070575 15 0.005445 -0.000034 9.057808e+02 -5.598919 0.036863 16 NaN NaN NaN NaN NaN 17 0.944659 -0.000002 8.626931e+06 -18.274646 1.811666 18 0.716954 0.000014 1.886910e+07 355.909897 1.221679 19 0.135161 0.000000 1.339936e+07 0.000000 0.683803 20 NaN NaN NaN NaN NaN 21 NaN NaN NaN NaN NaN 22 0.136553 0.000000 1.737642e+09 0.000000 0.378290 23 0.059149 0.000000 inf NaN 0.145721 24 0.034271 0.000000 inf NaN 0.045438 obs_t p95_3 p5_3 p95_mana_3 p5_mana_3 0 1.305501e+00 7.400077e+01 8.313594e-02 0.000000 0.000000 1 2.619782e-01 5.538810e+02 0.000000e+00 0.000000 0.000000 2 8.982157e-02 3.041716e+03 2.723007e+02 94.396331 70.650181 3 8.682545e-02 6.018847e+02 7.010078e+01 26.148634 0.749997 4 4.250739e-02 1.893629e+02 3.834790e+00 1.498805 0.000000 5 3.923567e-02 1.207974e+02 0.000000e+00 0.000000 46.059345 6 3.759947e-02 1.208297e+03 3.226699e+01 1.325350 1.196734 7 2.672979e-02 4.253935e+02 3.503701e+01 5.376322 0.055530 8 2.646569e-02 4.807638e+02 5.316203e+01 4.471793 0.000000 9 2.575924e-02 6.371754e+02 4.619459e+01 29.762485 0.152683 10 2.246766e-02 4.871114e+03 5.408793e+02 198.045508 207.691404 11 1.635207e-02 5.937378e+03 9.324097e+02 126.587539 95.143800 12 1.527527e-02 1.086093e+03 0.000000e+00 0.000000 0.000000 13 6.512101e-03 8.549834e+03 7.775513e+01 0.157804 0.000000 14 6.057680e-03 1.153847e+03 2.160361e+02 124.348527 0.009479 15 6.010994e-04 5.354649e+03 1.935241e+03 2081.349670 9.225513 16 2.030791e-04 2.054254e+03 0.000000e+00 0.000000 0.000000 17 1.095012e-05 1.654461e+07 5.344353e+04 0.000000 0.000000 18 3.799619e-06 3.184225e+07 2.562310e+06 0.000000 75.843273 19 1.008715e-06 6.726070e+07 0.000000e+00 0.000000 0.000000 20 6.815925e-07 4.885822e+05 0.000000e+00 0.000000 0.000000 21 7.452582e-08 2.297023e+07 0.000000e+00 0.000000 0.000000 22 7.858539e-09 4.804273e+09 0.000000e+00 0.000000 0.000000 23 0.000000e+00 NaN NaN NaN NaN 24 0.000000e+00 NaN NaN NaN NaN
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/2278170017.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 state_sum1_3['irri_t'] = state_sum1_3['irri_t'].fillna(0) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/2278170017.py:6: 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 state_sum1_3['irri_mana_t'] = state_sum1_3['irri_mana_t'].fillna(0) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/2278170017.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 state_sum0_3['irri_t'] = state_sum0_3['irri_t'].fillna(0) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/2278170017.py:22: 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 state_sum0_3['irri_mana_t'] = state_sum0_3['irri_mana_t'].fillna(0)
ex=state_sum3[['STATE1','sus_irri_mana_t','unsus_irri_mana_t']]
ex['irri_mana_t'] = ex['sus_irri_mana_t'] + ex['unsus_irri_mana_t']
print(ex)
STATE1 sus_irri_mana_t unsus_irri_mana_t irri_mana_t 0 Arkansas -6.272401e-01 -0.270238 -8.974779e-01 1 Kansas -3.420004e-02 -0.227739 -2.619393e-01 2 Illinois 1.736197e-01 -0.015525 1.580951e-01 3 North Carolina -7.354802e-02 -0.002142 -7.569037e-02 4 Georgia -3.320012e-02 -0.005357 -3.855741e-02 5 Delaware -3.035620e-02 -0.008878 -3.923404e-02 6 Tennessee -2.337716e-02 -0.013698 -3.707493e-02 7 Alabama -2.237756e-02 -0.000164 -2.254157e-02 8 South Carolina -1.839174e-02 -0.007249 -2.564113e-02 9 Maryland -2.067254e-02 -0.000210 -2.088300e-02 10 Indiana 7.394282e-02 -0.000275 7.366744e-02 11 Iowa 2.038336e-02 -0.001595 1.878799e-02 12 Virginia -1.302344e-02 -0.000549 -1.357199e-02 13 Kentucky -5.796825e-03 -0.000667 -6.463965e-03 14 Ohio 8.012802e-03 -0.000026 7.986328e-03 15 Pennsylvania 4.676175e-03 -0.000034 4.642520e-03 16 Florida -2.030441e-04 NaN NaN 17 Missouri -8.909902e-07 -0.000002 -2.892086e-06 18 Nebraska -3.507466e-07 0.000014 1.317247e-05 19 Mississippi -6.764142e-07 0.000000 -6.764142e-07 20 West Virginia -5.866308e-07 NaN NaN 21 New Jersey -7.004212e-09 NaN NaN 22 Louisiana 0.000000e+00 0.000000 0.000000e+00 23 Oklahoma 0.000000e+00 0.000000 0.000000e+00 24 Texas 0.000000e+00 0.000000 0.000000e+00
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/1214160529.py:2: 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 ex['irri_mana_t'] = ex['sus_irri_mana_t'] + ex['unsus_irri_mana_t']
# Calculate additional attributes
state_sum3['irri_mana_t'] = state_sum3['sus_irri_mana_t'] + state_sum3['unsus_irri_mana_t']
obs_t = state_sum3['obs_t'].sum()
sus_irri = state_sum3['sus_irri_t'].sum()
unsus_irri = state_sum3['unsus_irri_t'].sum()
sus_irri_mana = state_sum3['sus_irri_mana_t'].sum()
unsus_irri_mana = state_sum3['unsus_irri_mana_t'].sum()
print(sus_irri,unsus_irri,sus_irri_mana, unsus_irri_mana)
sus_irri_p=sus_irri/obs_t*100
unsus_irri_p=unsus_irri/obs_t*100
sus_irri_mana_p=sus_irri_mana/obs_t*100
unsus_irri_mana_p=unsus_irri_mana/obs_t*100
df_sum = pd.DataFrame({
'sus_irri_p': [sus_irri_p],
'unsus_irri_p': [unsus_irri_p],
'sus_irri_mana_p': [sus_irri_mana_p],
'unsus_irri_mana_p': [unsus_irri_mana_p]
})
print(df_sum)
df_sum['STATE1']='U.S.'
df_sum['t']=df_sum['sus_irri_p']+df_sum['unsus_irri_p']
df_sum['t_mana']=df_sum['sus_irri_mana_p']+df_sum['unsus_irri_mana_p']
df_sum=df_sum.merge(err_country3, left_on='STATE1', right_on='STATE1')
print(df_sum)
# Define the columns you want to plot
columns_to_plot = ['sus_irri_mana_p','unsus_irri_mana_p', 'sus_irri_p','unsus_irri_p']
columns_to_plot1 = ['t_mana', 'p5_mana_3','p95_mana_3','t','p5_3','p95_3']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['#fff59d', '#fff59d', '#b2ebf2', '#b2ebf2'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(df_sum['STATE1']))
# Create a figure and axis
fig, ax = plt.subplots(figsize=(5, 1)) # Adjust the figure size as needed
# Initialize left positions for each group
left_group1 = np.zeros(len(df_sum['STATE1']))
left_group2 = np.zeros(len(df_sum['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
if 'unsus_irri_mana_p' in columns_to_plot[i]:
hatch_pattern = '//'
elif 'unsus_irri_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, df_sum[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1, capsize=3, capstyle='round')
left_group1 += df_sum[columns_to_plot[i]]
main_data = df_sum[columns_to_plot1[0]]
error_lower = (df_sum[columns_to_plot1[ 1]]).values
error_upper = (df_sum[columns_to_plot1[ 2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, df_sum[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1, capsize=3, capstyle='round')
left_group2 += df_sum[columns_to_plot[i]]
main_data = df_sum[columns_to_plot1[3]]
error_lower = (df_sum[columns_to_plot1[4]]).values
error_upper = (df_sum[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
# Set y-tick labels
plt.yticks([0], df_sum['STATE1'], fontfamily='Arial')
# Set labels and title
ax.set_yticks(index)
ax.set_yticklabels(df_sum['STATE1'], fontfamily='Arial', fontsize=20)
ax.set_xticks(np.arange(-100, 900, 200))
ax.axvline(0, color='black', linestyle='--', linewidth=1)
ax.tick_params(axis='x', labelsize=20)
# Save the entire figure as a JPG with DPI 300
plt.savefig('national_soybean_irri_3.png', format='png', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
7.545351702332636 5.218298832223014 -0.6217544395264459 -0.554335744580312 sus_irri_p unsus_irri_p sus_irri_mana_p unsus_irri_mana_p 0 375.407736 259.628686 -30.934466 -27.580149 sus_irri_p unsus_irri_p sus_irri_mana_p unsus_irri_mana_p STATE1 \ 0 375.407736 259.628686 -30.934466 -27.580149 U.S. t t_mana p95_3 p5_3 p95_mana_3 p5_mana_3 0 635.036422 -58.514615 23.313356 26.06268 4.432222 4.157292
state_sum3_10=state_sum3.nlargest(10, 'obs_t')
state_sum3_10=state_sum3_10[['sus_gap_p', 'sus_gap_mana_p', 'unsus_gap_p', 'unsus_gap_mana_p',
'p5_mana_3','p95_mana_3','p5_3','p95_3','STATE1']]
state_sum3_10['unsus_gap_p'] = state_sum3_10['unsus_gap_p'].fillna(0)
state_sum3_10['unsus_gap_mana_p'] = state_sum3_10['unsus_gap_mana_p'].fillna(0)
state_sum3_10['gap_p']=state_sum3_10['sus_gap_p']+state_sum3_10['unsus_gap_p']
state_sum3_10['gap_mana_p']=state_sum3_10['sus_gap_mana_p']+state_sum3_10['unsus_gap_mana_p']
print(state_sum3_10)
state_sum3_10 = state_sum3_10.sort_values(by='gap_p', ascending=False)
# Define the columns you want to plot
columns_to_plot = ['sus_gap_mana_p', 'unsus_gap_mana_p', 'sus_gap_p', 'unsus_gap_p']
columns_to_plot1 = ['gap_mana_p', 'p5_mana_3','p95_mana_3','gap_p','p5_3','p95_3']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['#fff59d', '#fff59d', '#b2ebf2', '#b2ebf2'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(state_sum3_10['STATE1']))
# Expand the length of the y-axis (adjust the figure size)
plt.figure(figsize=(5, 12))
# Initialize left positions for each group
left_group1 = np.zeros(len(state_sum3_10['STATE1']))
left_group2 = np.zeros(len(state_sum3_10['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
std_err_col = None # Initialize std_err_col
if 'unsus_gap_mana_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add hatch for 'unsus_irri_mana_t' columns
elif 'unsus_gap_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, state_sum3_10[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1,capsize=3, capstyle='round')
left_group1 += state_sum3_10[columns_to_plot[i]]
main_data = state_sum3_10[columns_to_plot1[0]]
error_lower = (state_sum3_10[columns_to_plot1[1]]).values
error_upper = (state_sum3_10[columns_to_plot1[2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, state_sum3_10[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1,capsize=3, capstyle='round')
left_group2 += state_sum3_10[columns_to_plot[i]]
main_data = state_sum3_10[columns_to_plot1[3]]
error_lower = (state_sum3_10[columns_to_plot1[4]]).values
error_upper = (state_sum3_10[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[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 / 2, state_sum3_10['STATE1'], fontfamily='Arial')
# Set the x-axis label
plt.xlabel('Irrigation withdrawals (%)', fontfamily='Arial', fontsize=22)
# Set the x-ticks
x_ticks = np.arange(-200, 6500, 1600)
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
plt.text(0.85, 0.8, '(d) Soybean under\n3°C warming', 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=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('state_soybean_irri_3_10.png', format='png', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
sus_gap_p sus_gap_mana_p unsus_gap_p unsus_gap_mana_p p5_mana_3 \ 0 5.269971 -48.045948 0.943306 -20.699935 0.000000 1 101.588684 -13.054539 352.307143 -86.930621 0.000000 2 1806.642614 193.294011 1370.947990 -17.283819 70.650181 3 476.838954 -84.707903 52.327575 -2.467429 0.749997 4 98.240154 -78.104355 4.269414 -12.603192 0.000000 5 18.889968 -77.368899 32.065171 -22.626940 46.059345 6 780.594604 -62.174162 484.422300 -36.430755 1.196734 7 374.109274 -83.717695 5.121938 -0.613597 0.055530 8 369.482788 -69.492788 43.814997 -27.391644 0.000000 9 506.625873 -80.252891 30.567692 -0.817052 0.152683 p95_mana_3 p5_3 p95_3 STATE1 gap_p \ 0 0.000000 0.083136 74.000769 Arkansas 6.213277 1 0.000000 0.000000 553.880987 Kansas 453.895826 2 94.396331 272.300716 3041.715561 Illinois 3177.590604 3 26.148634 70.100777 601.884673 North Carolina 529.166530 4 1.498805 3.834790 189.362932 Georgia 102.509568 5 0.000000 0.000000 120.797402 Delaware 50.955139 6 1.325350 32.266988 1208.296658 Tennessee 1265.016904 7 5.376322 35.037013 425.393515 Alabama 379.231213 8 4.471793 53.162031 480.763848 South Carolina 413.297785 9 29.762485 46.194593 637.175395 Maryland 537.193565 gap_mana_p 0 -68.745882 1 -99.985160 2 176.010192 3 -87.175332 4 -90.707547 5 -99.995838 6 -98.604916 7 -84.331292 8 -96.884432 9 -81.069943
state_sum3_10_1 = state_sum3_10.tail(6)
# Define the columns you want to plot
columns_to_plot = ['sus_gap_mana_p', 'unsus_gap_mana_p', 'sus_gap_p', 'unsus_gap_p']
columns_to_plot1 = ['gap_mana_p', 'p5_mana_3','p95_mana_3','gap_p','p5_3','p95_3']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['#fff59d', '#fff59d', '#b2ebf2', '#b2ebf2'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(state_sum3_10_1['STATE1']))
# Expand the length of the y-axis (adjust the figure size)
plt.figure(figsize=(3, 6), facecolor='none')
# Initialize left positions for each group
left_group1 = np.zeros(len(state_sum3_10_1['STATE1']))
left_group2 = np.zeros(len(state_sum3_10_1['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
std_err_col = None # Initialize std_err_col
if 'unsus_gap_mana_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add hatch for 'unsus_irri_mana_t' columns
elif 'unsus_gap_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, state_sum3_10_1[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1,capsize=3, capstyle='round')
left_group1 += state_sum3_10_1[columns_to_plot[i]]
main_data = state_sum3_10_1[columns_to_plot1[0]]
error_lower = (state_sum3_10_1[columns_to_plot1[1]]).values
error_upper = (state_sum3_10_1[columns_to_plot1[2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, state_sum3_10_1[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1,capsize=3, capstyle='round')
left_group2 += state_sum3_10_1[columns_to_plot[i]]
main_data = state_sum3_10_1[columns_to_plot1[3]]
error_lower = (state_sum3_10_1[columns_to_plot1[4]]).values
error_upper = (state_sum3_10_1[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[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 / 2, state_sum3_10_1['STATE1'], fontfamily='Arial')
# Set the x-ticks
x_ticks = np.arange(-200, 1400, 500)
plt.xticks(x_ticks, fontfamily='Arial')
# Add a dashed line at x=0
ax = plt.gca()
ax.axvline(0, color='black', linestyle='--', linewidth=1)
# Set tick label font sizes
ax.tick_params(axis='both', labelsize=16)
ax.set_xticklabels(ax.get_xticklabels(), fontfamily='Arial')
ax.set_yticklabels(ax.get_yticklabels(), fontfamily='Arial')
ax.spines['bottom'].set_linewidth(1.5) # Set x-axis line width
ax.spines['left'].set_linewidth(1.5) # Set y-axis line width
ax.spines['right'].set_linewidth(1.5) # Set right axis line width
ax.spines['top'].set_linewidth(1.5) # Set top axis line width
# Save the entire figure as a PNG with DPI 300 and transparent background
plt.savefig('state_soybean_irri_3_10_tail.png', format='png', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
state_sum3['irri_mana_t3'] = state_sum3['sus_irri_mana_t'] + state_sum3['unsus_irri_mana_t']
state_sum15['irri_mana_t15'] = state_sum15['sus_irri_mana_t'] + state_sum15['unsus_irri_mana_t']
obs_t = state_sum3['obs_t'].sum()
sus_irri_mana3 = state_sum3['sus_irri_mana_t'].sum()
unsus_irri_mana3 = state_sum3['unsus_irri_mana_t'].sum()
sus_irri_mana15 = state_sum15['sus_irri_mana_t'].sum()
unsus_irri_mana15 = state_sum15['unsus_irri_mana_t'].sum()
print(sus_irri_mana15, unsus_irri_mana15,sus_irri_mana3, unsus_irri_mana3)
sus_irri_mana_p15=sus_irri_mana15/obs_t*100
unsus_irri_mana_p15=unsus_irri_mana15/obs_t*100
sus_irri_mana_p3=sus_irri_mana3/obs_t*100
unsus_irri_mana_p3=unsus_irri_mana3/obs_t*100
df_sum = pd.DataFrame({
'sus_irri_mana_p15': [sus_irri_mana_p15],
'unsus_irri_mana_p15': [unsus_irri_mana_p15],
'sus_irri_mana_p3': [sus_irri_mana_p3],
'unsus_irri_mana_p3': [unsus_irri_mana_p3]
})
print(df_sum)
df_sum['STATE1']='U.S.'
df_sum['t_mana15']=df_sum['sus_irri_mana_p15']+df_sum['unsus_irri_mana_p15']
df_sum['t_mana3']=df_sum['sus_irri_mana_p3']+df_sum['unsus_irri_mana_p3']
df_sum=df_sum.merge(err_country, left_on='STATE1', right_on='STATE1')
print(df_sum)
# Define the columns you want to plot
columns_to_plot = ['sus_irri_mana_p15','unsus_irri_mana_p15', 'sus_irri_mana_p3','unsus_irri_mana_p3']
columns_to_plot1 = ['t_mana15', 'p5_mana_15','p95_mana_15','t_mana3','p5_mana_3','p95_mana_3']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['cadetblue', 'cadetblue', 'lightsalmon', 'lightsalmon'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(df_sum['STATE1']))
# Create a figure and axis
fig, ax = plt.subplots(figsize=(5, 1)) # Adjust the figure size as needed
# Initialize left positions for each group
left_group1 = np.zeros(len(df_sum['STATE1']))
left_group2 = np.zeros(len(df_sum['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
if 'unsus_irri_mana_p' in columns_to_plot[i]:
hatch_pattern = '//'
elif 'unsus_irri_p' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, df_sum[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1, capsize=3, capstyle='round')
left_group1 += df_sum[columns_to_plot[i]]
main_data = df_sum[columns_to_plot1[0]]
error_lower = (df_sum[columns_to_plot1[ 1]]).values
error_upper = (df_sum[columns_to_plot1[ 2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, df_sum[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1, capsize=3, capstyle='round')
left_group2 += df_sum[columns_to_plot[i]]
main_data = df_sum[columns_to_plot1[3]]
error_lower = (df_sum[columns_to_plot1[4]]).values
error_upper = (df_sum[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
# Set y-tick labels
plt.yticks([0], df_sum['STATE1'], fontfamily='Arial')
# Set labels and title
ax.set_yticks(index)
ax.set_yticklabels(df_sum['STATE1'], fontfamily='Arial', fontsize=24)
ax.set_xticks(np.arange(-80, 10, 20))
ax.axvline(0, color='black', linestyle='--', linewidth=1)
ax.tick_params(axis='x', labelsize=24)
# Save the entire figure as a JPG with DPI 300
plt.savefig('national_soybean_irri.svg', format='svg', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
-0.789165656222785 -0.5220953863398782 -0.6217544395264459 -0.554335744580312 sus_irri_mana_p15 unsus_irri_mana_p15 sus_irri_mana_p3 \ 0 -39.263762 -25.976078 -30.934466 unsus_irri_mana_p3 0 -27.580149 sus_irri_mana_p15 unsus_irri_mana_p15 sus_irri_mana_p3 \ 0 -39.263762 -25.976078 -30.934466 unsus_irri_mana_p3 STATE1 t_mana15 t_mana3 p95_15 p5_15 \ 0 -27.580149 U.S. -65.23984 -58.514615 41.683722 31.921113 p95_mana_15 p5_mana_15 p95_3 p5_3 p95_mana_3 p5_mana_3 0 4.534483 4.328059 23.313356 26.06268 4.432222 4.157292
print(sus_irri_mana15+unsus_irri_mana15)
print(sus_irri_mana3+unsus_irri_mana3)
print(df_sum['t_mana15'])
print(df_sum['t_mana15']-df_sum['p5_mana_15'])
print(df_sum['t_mana15']+df_sum['p95_mana_15'])
print(df_sum['t_mana3'])
print(df_sum['t_mana3']-df_sum['p5_mana_3'])
print(df_sum['t_mana3']+df_sum['p95_mana_3'])
-1.3112610425626632 -1.176090184106758 0 -65.23984 Name: t_mana15, dtype: float64 0 -69.567899 dtype: float64 0 -60.705357 dtype: float64 0 -58.514615 Name: t_mana3, dtype: float64 0 -62.671907 dtype: float64 0 -54.082394 dtype: float64
ds3_10=state_sum3_10[[ 'sus_gap_mana_p', 'unsus_gap_mana_p',
'p5_mana_3','p95_mana_3','gap_mana_p','STATE1']]
ds3_10.rename(columns={'sus_gap_mana_p': 'sus_gap_mana_p3'}, inplace=True)
ds3_10.rename(columns={'unsus_gap_mana_p': 'unsus_gap_mana_p3'}, inplace=True)
ds3_10.rename(columns={'gap_mana_p': 'gap_mana_p3'}, inplace=True)
ds15_10=state_sum15_10[[ 'sus_gap_mana_p', 'unsus_gap_mana_p',
'p5_mana_15','p95_mana_15','gap_mana_p','STATE1']]
ds15_10.rename(columns={'sus_gap_mana_p': 'sus_gap_mana_p15'}, inplace=True)
ds15_10.rename(columns={'unsus_gap_mana_p': 'unsus_gap_mana_p15'}, inplace=True)
ds15_10.rename(columns={'gap_mana_p': 'gap_mana_p15'}, inplace=True)
ds = ds3_10.merge(ds15_10, on='STATE1', how='left')
ds['sus_gap_mana_p15'] = ds['sus_gap_mana_p15'].fillna(0)
ds['unsus_gap_mana_p15'] = ds['unsus_gap_mana_p15'].fillna(0)
ds['p5_mana_15'] = ds['p5_mana_15'].fillna(0)
ds['p95_mana_15'] = ds['p95_mana_15'].fillna(0)
ds['gap_mana_p15'] = ds['gap_mana_p15'].fillna(0)
print(ds)
sus_gap_mana_p3 unsus_gap_mana_p3 p5_mana_3 p95_mana_3 gap_mana_p3 \ 0 193.294011 -17.283819 70.650181 94.396331 176.010192 1 -62.174162 -36.430755 1.196734 1.325350 -98.604916 2 -80.252891 -0.817052 0.152683 29.762485 -81.069943 3 -84.707903 -2.467429 0.749997 26.148634 -87.175332 4 -13.054539 -86.930621 0.000000 0.000000 -99.985160 5 -69.492788 -27.391644 0.000000 4.471793 -96.884432 6 -83.717695 -0.613597 0.055530 5.376322 -84.331292 7 -78.104355 -12.603192 0.000000 1.498805 -90.707547 8 -77.368899 -22.626940 46.059345 0.000000 -99.995838 9 -48.045948 -20.699935 0.000000 0.000000 -68.745882 STATE1 sus_gap_mana_p15 unsus_gap_mana_p15 p5_mana_15 \ 0 Illinois 332.095216 -13.376365 81.001034 1 Tennessee -46.961224 -8.360729 29.966272 2 Maryland 0.000000 0.000000 0.000000 3 North Carolina -75.747176 -1.695715 1.117400 4 Kansas -13.054539 -86.786803 0.000000 5 South Carolina -9.476163 -0.005217 0.000000 6 Alabama -95.290298 -0.613597 3.286652 7 Georgia -24.765330 -4.226071 4.123399 8 Delaware -6.926697 -22.626940 12.255505 9 Arkansas -79.642657 -20.312419 0.000000 p95_mana_15 gap_mana_p15 0 89.580134 318.718851 1 99.300143 -55.321953 2 0.000000 0.000000 3 3.427073 -77.442891 4 0.000000 -99.841342 5 0.124535 -9.481380 6 27.001409 -95.903895 7 3.597527 -28.991401 8 5.031586 -29.553637 9 0.374630 -99.955076
/var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/450882398.py:3: 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 ds3_10.rename(columns={'sus_gap_mana_p': 'sus_gap_mana_p3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/450882398.py:4: 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 ds3_10.rename(columns={'unsus_gap_mana_p': 'unsus_gap_mana_p3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/450882398.py:5: 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 ds3_10.rename(columns={'gap_mana_p': 'gap_mana_p3'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/450882398.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 ds15_10.rename(columns={'sus_gap_mana_p': 'sus_gap_mana_p15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/450882398.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 ds15_10.rename(columns={'unsus_gap_mana_p': 'unsus_gap_mana_p15'}, inplace=True) /var/folders/vd/0_phd7hx2n51y4412862zww00000gp/T/ipykernel_14618/450882398.py:10: 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 ds15_10.rename(columns={'gap_mana_p': 'gap_mana_p15'}, inplace=True)
ds = ds.sort_values(by='gap_mana_p3', ascending=False)
print(ds)
output_csv_file = "soybean_figure3.csv"
ds.to_csv(output_csv_file, index=False)
# Define the columns you want to plot
columns_to_plot = ['sus_gap_mana_p15', 'unsus_gap_mana_p15', 'sus_gap_mana_p3', 'unsus_gap_mana_p3']
columns_to_plot1 = ['gap_mana_p15', 'p5_mana_15','p95_mana_15','gap_mana_p3', 'p5_mana_3','p95_mana_3']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['cadetblue', 'cadetblue', 'lightsalmon', 'lightsalmon'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(ds['STATE1']))
# Expand the length of the y-axis (adjust the figure size)
plt.figure(figsize=(5, 12))
# Initialize left positions for each group
left_group1 = np.zeros(len(ds['STATE1']))
left_group2 = np.zeros(len(ds['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
std_err_col = None # Initialize std_err_col
if 'unsus_gap_mana_p15' in columns_to_plot[i]:
hatch_pattern = '//' # Add hatch for 'unsus_irri_mana_t' columns
elif 'unsus_gap_mana_p3' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, ds[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1,capsize=3, capstyle='round')
left_group1 += ds[columns_to_plot[i]]
main_data = ds[columns_to_plot1[0]]
error_lower = (ds[columns_to_plot1[1]]).values
error_upper = (ds[columns_to_plot1[2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, ds[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1,capsize=3, capstyle='round')
left_group2 += ds[columns_to_plot[i]]
main_data = ds[columns_to_plot1[3]]
error_lower = (ds[columns_to_plot1[4]]).values
error_upper = (ds[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[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 / 2, ds['STATE1'], fontfamily='Arial')
# Set the x-axis label
plt.xlabel('Irrigation withdrawals (%)', fontfamily='Arial', fontsize=24)
# Set the x-ticks
x_ticks = np.arange(-200, 900, 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
plt.text(0.8, 0.82, '(d) Soybean', 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_soybean_irri.svg', format='svg', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
sus_gap_mana_p3 unsus_gap_mana_p3 p5_mana_3 p95_mana_3 gap_mana_p3 \ 0 193.294011 -17.283819 70.650181 94.396331 176.010192 9 -48.045948 -20.699935 0.000000 0.000000 -68.745882 2 -80.252891 -0.817052 0.152683 29.762485 -81.069943 6 -83.717695 -0.613597 0.055530 5.376322 -84.331292 3 -84.707903 -2.467429 0.749997 26.148634 -87.175332 7 -78.104355 -12.603192 0.000000 1.498805 -90.707547 5 -69.492788 -27.391644 0.000000 4.471793 -96.884432 1 -62.174162 -36.430755 1.196734 1.325350 -98.604916 4 -13.054539 -86.930621 0.000000 0.000000 -99.985160 8 -77.368899 -22.626940 46.059345 0.000000 -99.995838 STATE1 sus_gap_mana_p15 unsus_gap_mana_p15 p5_mana_15 \ 0 Illinois 332.095216 -13.376365 81.001034 9 Arkansas -79.642657 -20.312419 0.000000 2 Maryland 0.000000 0.000000 0.000000 6 Alabama -95.290298 -0.613597 3.286652 3 North Carolina -75.747176 -1.695715 1.117400 7 Georgia -24.765330 -4.226071 4.123399 5 South Carolina -9.476163 -0.005217 0.000000 1 Tennessee -46.961224 -8.360729 29.966272 4 Kansas -13.054539 -86.786803 0.000000 8 Delaware -6.926697 -22.626940 12.255505 p95_mana_15 gap_mana_p15 0 89.580134 318.718851 9 0.374630 -99.955076 2 0.000000 0.000000 6 27.001409 -95.903895 3 3.427073 -77.442891 7 3.597527 -28.991401 5 0.124535 -9.481380 1 99.300143 -55.321953 4 0.000000 -99.841342 8 5.031586 -29.553637
ds_1 = ds.tail(6)
print(ds_1)
# Define the columns you want to plot
columns_to_plot = ['sus_gap_mana_p15', 'unsus_gap_mana_p15', 'sus_gap_mana_p3', 'unsus_gap_mana_p3']
columns_to_plot1 = ['gap_mana_p15', 'p5_mana_15','p95_mana_15','gap_mana_p3', 'p5_mana_3','p95_mana_3']
# Number of attributes
num_attributes = len(columns_to_plot)
# Define the colors you want for the bars
colors = ['cadetblue', 'cadetblue', 'lightsalmon', 'lightsalmon'] # Add more colors if needed
# Set the width of each bar
bar_width = 0.4
# Set the positions of the bars on the y-axis
index = np.arange(len(ds_1['STATE1']))
# Expand the length of the y-axis (adjust the figure size)
plt.figure(figsize=(3, 6), facecolor='none')
# Initialize left positions for each group
left_group1 = np.zeros(len(ds_1['STATE1']))
left_group2 = np.zeros(len(ds_1['STATE1']))
# Create a bar for each attribute
for i in range(num_attributes):
hatch_pattern = '' # Initialize hatch pattern
std_err_col = None # Initialize std_err_col
if 'unsus_gap_mana_p15' in columns_to_plot[i]:
hatch_pattern = '//' # Add hatch for 'unsus_irri_mana_t' columns
elif 'unsus_gap_mana_p3' in columns_to_plot[i]:
hatch_pattern = '//' # Add a different hatch for 'unsus_irri_t' columns
if i < 2: # First two columns
plt.barh(index, ds_1[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group1, zorder=1,capsize=3, capstyle='round')
left_group1 += ds_1[columns_to_plot[i]]
main_data = ds_1[columns_to_plot1[0]]
error_lower = (ds_1[columns_to_plot1[1]]).values
error_upper = (ds_1[columns_to_plot1[2]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[i],alpha = 0.6,
linewidth=2, capthick=2, capsize = 3, markersize=5)
else: # Latter two columns
plt.barh(index + bar_width, ds_1[columns_to_plot[i]],
bar_width, label=f'{columns_to_plot[i]}', color=colors[i], hatch=hatch_pattern,
edgecolor='black', left=left_group2, zorder=1,capsize=3, capstyle='round')
left_group2 += ds_1[columns_to_plot[i]]
main_data = ds_1[columns_to_plot1[3]]
error_lower = (ds_1[columns_to_plot1[4]]).values
error_upper = (ds_1[columns_to_plot1[5]]).values
xerr = [error_lower, error_upper] # Correct error shape
plt.errorbar(main_data, index+bar_width, xerr=np.array(xerr),
fmt='o', color='black', label=columns_to_plot1[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 / 2, ds_1['STATE1'], fontfamily='Arial')
# Set the x-ticks
x_ticks = np.arange(-150, 60, 100)
plt.xticks(x_ticks, fontfamily='Arial')
# Add a dashed line at x=0
ax = plt.gca()
ax.axvline(0, color='black', linestyle='--', linewidth=1)
# Set tick label font sizes
ax.tick_params(axis='both', labelsize=26)
ax.set_xticklabels(ax.get_xticklabels(), fontfamily='Arial')
ax.set_yticklabels(ax.get_yticklabels(), fontfamily='Arial')
ax.spines['bottom'].set_linewidth(1.5) # Set x-axis line width
ax.spines['left'].set_linewidth(1.5) # Set y-axis line width
ax.spines['right'].set_linewidth(1.5) # Set right axis line width
ax.spines['top'].set_linewidth(1.5) # Set top axis line width
# Save the entire figure as a PNG with DPI 300 and transparent background
plt.savefig('state_soybean_irri_tail.svg', format='svg', dpi=300, bbox_inches='tight',
pad_inches=0.02, transparent=True)
# Show the plot
plt.show()
sus_gap_mana_p3 unsus_gap_mana_p3 p5_mana_3 p95_mana_3 gap_mana_p3 \ 3 -84.707903 -2.467429 0.749997 26.148634 -87.175332 7 -78.104355 -12.603192 0.000000 1.498805 -90.707547 5 -69.492788 -27.391644 0.000000 4.471793 -96.884432 1 -62.174162 -36.430755 1.196734 1.325350 -98.604916 4 -13.054539 -86.930621 0.000000 0.000000 -99.985160 8 -77.368899 -22.626940 46.059345 0.000000 -99.995838 STATE1 sus_gap_mana_p15 unsus_gap_mana_p15 p5_mana_15 \ 3 North Carolina -75.747176 -1.695715 1.117400 7 Georgia -24.765330 -4.226071 4.123399 5 South Carolina -9.476163 -0.005217 0.000000 1 Tennessee -46.961224 -8.360729 29.966272 4 Kansas -13.054539 -86.786803 0.000000 8 Delaware -6.926697 -22.626940 12.255505 p95_mana_15 gap_mana_p15 3 3.427073 -77.442891 7 3.597527 -28.991401 5 0.124535 -9.481380 1 99.300143 -55.321953 4 0.000000 -99.841342 8 5.031586 -29.553637