- 积分
- 387
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2020-3-11
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
代码如下,我理解的是先读出nc文件中的变量情况,然后加载shapefile后把区域尺度缩小到湖北省的。但是套用CSDN下的案例,没有画出湖北省市界,请各位小伙伴和前辈帮忙指正下哪里出现了问题。附件为出图情况
import netCDF4 as nc
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap
import numpy as np
#读nc,提变量
plt.rc('font',family='Times New Roman',size=16)# 全局字体为新罗马
obj=nc.Dataset('simulation\\data_201501.nc')
sn=obj.variables['sn']
se=obj.variables['se']
lats=obj.variables['lat'][:];lons=obj.variables['lon'][:]
zong=obj.variables['2015_01'][1,3,::,::]
# Plot Data
fig, ax = plt.subplots()#figure为画布,axes为绘图区
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) #meshgrid是一个numpy函数,把两个数组转化为一个矩阵。
#加海岸线,国境线,洲际线
m.drawcoastlines()
m.drawstates()
m.drawcountries()
# 添加shape文件
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, np.squeeze(zong), cmap='rainbow')#rainbow
# Add Colorbar
cbar = m.colorbar(cs, location='bottom', pad="10%")
plt.show() |
-
|