Python-⽓象-⼤⽓科学-可视化绘图系列(⼆)——利⽤basemap叠加地图,并
添加⽩。。。
1⽩化单图代码:
2import numpy as np
3import xarray as xr
4from mpl_toolkits.basemap import Basemap
addressee5import matplotlib.pyplot as plt
6from matplotlib.patches import Polygon
7import matplotlib.patches as mpatches
8
9 ds = xr.open_dataset('')
10 t =  ds['value']
11 lons = ds.lon.data
12 lats = ds.lat.data
13 temp = xr.DataArray(t.data.T, coords=[lats,lons], dims=['latitude','longitude'])
14# 创建画图空间
15 fig, ax = plt.subplots()
16 m = Basemap(projection='cyl',resolution='i',llcrnrlon=lons.min(),llcrnrlat=lats.min(),
17        urcrnrlon=lons.max(),urcrnrlat=lats.max(),lon_0=120.,lat_0=90)
18 Lon,Lat = np.meshgrid(lons[:],lats[:])
19 X,Y = m(Lon,Lat)
20
学单片机毕业了做什么工作啊21 shp_info3 = m.readshapefile("CHN_adm_shp\\CHN_adm3",'states',drawbounds=False,linewidth = 0.4,zorder=10)
22for info, shp in zip(m.states_info, m.states):
23    proid = info['NAME_1']  # 可以⽤notepad打开CHN_adm1.csv⽂件,可以知道'NAME_1'代表各省的名称
24if proid == 'Hubei':
selector是什么开关
25        poly = Polygon(shp,facecolor='None',edgecolor='b', lw=0.8)
26        ax.add_patch(poly)
27else:
28        poly = Polygon(shp,facecolor='w',edgecolor='w', lw=0.8)
29        ax.add_patch(poly)
30
31#市⼀级底图
32 shp_info2 = m.readshapefile("CHN_adm_shp\\CHN_adm2",'states',drawbounds=False,linewidth = 0.4,zorder=10)
33for info, shp in zip(m.states_info, m.states):
34    proid = info['NAME_1']
35if proid == 'Hubei':
36        poly = Polygon(shp,facecolor='None',edgecolor='black', lw=0.8)
37        ax.add_patch(poly)
38 urf(Lon,Lat,t.data.T,np.arange(0,1.1,0.1),cmap='rainbow')
39 legend_elements = [mpatches.Patch(facecolor='#a50026',label='0-0.1'),
40                    mpatches.Patch(facecolor='#da362a',label='0.1-0.2'),
41                    mpatches.Patch(facecolor='#f67a49',label='0.2-0.3'),
42                    mpatches.Patch(facecolor='#fdbf6f',label='0.3-0.4'),
43                    mpatches.Patch(facecolor='#feeda1',label='0.4-0.5'),
44                    mpatches.Patch(facecolor='#ebf7a3',label='0.5-0.6'),
45                    mpatches.Patch(facecolor='#b7e075',label='0.6-0.7'),
46                    mpatches.Patch(facecolor='#75c465',label='0.7-0.8'),
html5流星动画47                    mpatches.Patch(facecolor='#249d53',label='0.8-0.9'),
48                    mpatches.Patch(facecolor='#006837',label='0.9-1')]
49 plt.legend(handles=legend_elements, loc='lower right',title='NDVI')
50# cbar = lorbar(cs,orientation='horizontal',label='Potential')
个例效果:
1多图代码:
2import numpy as np
3import xarray as xr
4from mpl_toolkits.basemap import Basemap
5import matplotlib.pyplot as plt
6from matplotlib.patches import Polygon
7import matplotlib.patches as mpatches
8
9 ds = xr.open_dataset('')
10 t =  ds['value']
11 lons = ds.lon.data
12 lats = ds.lat.data
13 temp = xr.DataArray(t.data.T, coords=[lats,lons], dims=['latitude','longitude'])
14# 创建画图空间
15 fig, ax = plt.subplots(2,2,figsize=(15,15))
16for ii in np.arange(4):
17print('ii:::::',ii)
linux系统编程慕课版
18    plt.sca(ax[np.unravel_index(ii,(2,2))]) # 获取⼀维索引在⼆维矩阵中的索引
19    m = Basemap(projection='cyl',resolution='i',llcrnrlon=lons.min(),llcrnrlat=lats.min(),
20        urcrnrlon=lons.max(),urcrnrlat=lats.max(),lon_0=120.,lat_0=90)
21    Lon,Lat = np.meshgrid(lons[:],lats[:])
22    X,Y = m(Lon,Lat)
23
24    shp_info3 = m.readshapefile("CHN_adm_shp\\CHN_adm3",'states',drawbounds=False,linewidth = 0.4,zorder=10) 25for info, shp in zip(m.states_info, m.states):
26        proid = info['NAME_1']  # 可以⽤notepad打开CHN_adm1.csv⽂件,可以知道'NAME_1'代表各省的名称
27if proid == 'Hubei':
28            poly = Polygon(shp,facecolor='None',edgecolor='b', lw=0.8)
29            ax[np.unravel_index(ii,(2,2))].add_patch(poly)
30else:
31            poly = Polygon(shp,facecolor='w',edgecolor='w', lw=0.8)
32            ax[np.unravel_index(ii,(2,2))].add_patch(poly)
33
34#市⼀级底图
气象python零基础入门教程35    shp_info2 = m.readshapefile("CHN_adm_shp\\CHN_adm2",'states',drawbounds=False,linewidth = 0.4,zorder=10) 36for info, shp in zip(m.states_info, m.states):
37        proid = info['NAME_1']
38if proid == 'Hubei':
39            poly = Polygon(shp,facecolor='None',edgecolor='black', lw=0.8)
40            ax[np.unravel_index(ii,(2,2))].add_patch(poly)
41
42    urf(Lon,Lat,t.data.T,np.arange(0,1.1,0.1),cmap='rainbow')
43
44 cbar = lorbar(cs, ax=ax.ravel().tolist(), orientation='horizontal')
45 plt.savefig('multi_masked.jpg',bbox_inches = 'tight')
效果如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。