本文为原创文章:https://www.jianshu.com/p/2bd429da2263
白化单图代码:
import numpy as np
import xarray as xr
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.patches as mpatches
ds = xr.open_dataset('2019072300.006.nc')
t = ds['value']
lons = ds.lon.data
lats = ds.lat.data
temp = xr.DataArray(t.data.T, coords=[lats,lons], dims=['latitude','longitude'])
# 创建画图空间
fig, ax = plt.subplots()
m = Basemap(projection='cyl',resolution='i',llcrnrlon=lons.min(),llcrnrlat=lats.min(),
urcrnrlon=lons.max(),urcrnrlat=lats.max(),lon_0=120.,lat_0=90)
Lon,Lat = np.meshgrid(lons[:],lats[:])
X,Y = m(Lon,Lat)
shp_info3 = m.readshapefile("CHN_adm_shp\\CHN_adm3",'states',drawbounds=False,linewidth = 0.4,zorder=10)
for info, shp in zip(m.states_info, m.states):
proid = info['NAME_1'] # 可以用notepad打开CHN_adm1.csv文件,可以知道'NAME_1'代表各省的名称
if proid == 'Hubei':
poly = Polygon(shp,facecolor='None',edgecolor='b', lw=0.8)
ax.add_patch(poly)
else:
poly = Polygon(shp,facecolor='w',edgecolor='w', lw=0.8)
ax.add_patch(poly)
#市一级底图
shp_info2 = m.readshapefile("CHN_adm_shp\\CHN_adm2",'states',drawbounds=False,linewidth = 0.4,zorder=10)
for info, shp in zip(m.states_info, m.states):
proid = info['NAME_1']
if proid == 'Hubei':
poly = Polygon(shp,facecolor='None',edgecolor='black', lw=0.8)
ax.add_patch(poly)
cs=m.contourf(Lon,Lat,t.data.T,np.arange(0,1.1,0.1),cmap='rainbow')
legend_elements = [mpatches.Patch(facecolor='#a50026',label='0-0.1'),
mpatches.Patch(facecolor='#da362a',label='0.1-0.2'),
mpatches.Patch(facecolor='#f67a49',label='0.2-0.3'),
mpatches.Patch(facecolor='#fdbf6f',label='0.3-0.4'),
mpatches.Patch(facecolor='#feeda1',label='0.4-0.5'),
mpatches.Patch(facecolor='#ebf7a3',label='0.5-0.6'),
mpatches.Patch(facecolor='#b7e075',label='0.6-0.7'),
mpatches.Patch(facecolor='#75c465',label='0.7-0.8'),
mpatches.Patch(facecolor='#249d53',label='0.8-0.9'),
mpatches.Patch(facecolor='#006837',label='0.9-1')]
plt.legend(handles=legend_elements, loc='lower right',title='NDVI')
# cbar = plt.colorbar(cs,orientation='horizontal',label='Potential')
著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处。
作者:斩羚_HermionX
多图代码:
import numpy as np
import xarray as xr
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.patches as mpatches
ds = xr.open_dataset('2019072300.006.nc')
t = ds['value']
lons = ds.lon.data
lats = ds.lat.data
temp = xr.DataArray(t.data.T, coords=[lats,lons], dims=['latitude','longitude'])
# 创建画图空间
fig, ax = plt.subplots(2,2,figsize=(15,15))
for ii in np.arange(4):
print('ii:::::',ii)
plt.sca(ax[np.unravel_index(ii,(2,2))]) # 获取一维索引在二维矩阵中的索引
m = Basemap(projection='cyl',resolution='i',llcrnrlon=lons.min(),llcrnrlat=lats.min(),
urcrnrlon=lons.max(),urcrnrlat=lats.max(),lon_0=120.,lat_0=90)
Lon,Lat = np.meshgrid(lons[:],lats[:])
X,Y = m(Lon,Lat)
shp_info3 = m.readshapefile("CHN_adm_shp\\CHN_adm3",'states',drawbounds=False,linewidth = 0.4,zorder=10)
for info, shp in zip(m.states_info, m.states):
proid = info['NAME_1'] # 可以用notepad打开CHN_adm1.csv文件,可以知道'NAME_1'代表各省的名称
if proid == 'Hubei':
poly = Polygon(shp,facecolor='None',edgecolor='b', lw=0.8)
ax[np.unravel_index(ii,(2,2))].add_patch(poly)
else:
poly = Polygon(shp,facecolor='w',edgecolor='w', lw=0.8)
ax[np.unravel_index(ii,(2,2))].add_patch(poly)
#市一级底图
shp_info2 = m.readshapefile("CHN_adm_shp\\CHN_adm2",'states',drawbounds=False,linewidth = 0.4,zorder=10)
for info, shp in zip(m.states_info, m.states):
proid = info['NAME_1']
if proid == 'Hubei':
poly = Polygon(shp,facecolor='None',edgecolor='black', lw=0.8)
ax[np.unravel_index(ii,(2,2))].add_patch(poly)
cs=m.contourf(Lon,Lat,t.data.T,np.arange(0,1.1,0.1),cmap='rainbow')
cbar = plt.colorbar(cs, ax=ax.ravel().tolist(), orientation='horizontal')
plt.savefig('multi_masked.jpg',bbox_inches = 'tight')
---------------------
著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处。
作者:斩羚_HermionX