I have a shapefile of the State of India called Rajasthan and I want to plot precipitation data over it only. I am unable to find a solution anywhere. I am providing the code as well as the plot down below. Basically what I am doing over here is creating a plot of precipitation rate averaging over 10 years, but I want to plot data over the Rajasthan state only(Shapefile).
Code:-
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
filename = (r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19790101-19791231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19800101-19801231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19810101-19811231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19820101-19821231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19830101-19831231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19840101-19841231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19850101-19851231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19860101-19861231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19870101-19871231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19880101-19881231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19890101-19891231.nc ',r'F:\thesis\sourcefiles\totalprecip1979-2020\ncum_imdaa_reanl_DY_APCP-sfc_19900101-19901231.nc ')
ds = xr.open_mfdataset(filename)
monthly = ds.resample(time='m').mean()
lats = monthly.variables['latitude'][:]
lons = monthly.variables['longitude'][:]
time = monthly.variables['time'][:]
ap = monthly.variables['APCP-sfc'][:]
mp = Basemap(projection='merc',
llcrnrlon=69,
llcrnrlat=22.5,
urcrnrlon=79,
urcrnrlat=31,
resolution='i')
longitude, latitude = np.meshgrid(lons, lats)
x, y = mp(longitude, latitude)
mon = np.arange(0, 12)
nam = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November',
'December']
for i in mon:
c_scheme = mp.pcolor(x, y, np.squeeze(ap[i, :, :]), cmap='Blues', shading='auto')
mp.drawcoastlines()
mp.drawstates()
mp.readshapefile(r'F:\thesis\India Shape\countrieswithrajsthan', 'countrieswithrajsthan')
cbar = mp.colorbar(c_scheme, location='right', pad='10%', label='mm/day')
mon = i + 1
plt.title('Monthly mean plot of precipitation rate(mm/day) of state Rajasthan\n from year 1979 to 1990\n' + nam[i], y=0.99)
plt.xlabel('longitude', labelpad= 15)
plt.ylabel('latitude', labelpad= 35)
plt.clim(0, 25)
parallels = np.arange(0., 81, 2.)
mp.drawparallels(parallels, labels=[True, False, False, False])
meridians = np.arange(10., 351., 2.)
mp.drawmeridians(meridians, labels=[False, False, False, True])
plt.savefig(r'F:\thesis\Plots\plot of totalpreciprajsthan 1979-2020' + '//' + str(i) + '79-90.jpg')
plt.clf()
Here is the plot for a month: -