我有这个 python 脚本,它绘制了位势高度的等高线图 -
nc_f = './hgt_500_2014_12_5_00Z.nc' # Your filename
nc_fid = Dataset(nc_f, 'r')
lats = nc_fid.variables['lat'][:] # extract/copy the data
lons = nc_fid.variables['lon'][:]
time = nc_fid.variables['time'][:]
hgt = nc_fid.variables['hgt'][:] # shape is time, lat, lon as shown above
x, y = np.meshgrid(lons, lats,copy=False)
rbf = scipy.interpolate.Rbf(x, y, hgt, function='linear')
zi = rbf(x, y)
plt.contour(x,y,zi)
plt.show()
我希望能够将这个情节与海岸线和国家叠加。
我试过了,但这给了我海岸线和国家,但缺少地势高度等高线
m = Basemap(width=5000000,height=3500000,
resolution='l',projection='stere',\
lat_ts=40,lat_0=lat_0,lon_0=lon_0)
x, y = np.meshgrid(lons, lats,copy=False)
rbf = scipy.interpolate.Rbf(x, y, hgt, function='linear')
zi = rbf(x, y)
cs = m.pcolor(x,y,np.squeeze(hgt))
m.drawcoastlines()
m.drawcountries()
cs = m.contour(x,y,zi,15,linewidths=1.5)
cbar = m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(hgt_units)
plt.title('500 hPa Geopotential Height')
plt.savefig('testplot.png')
plt.show()
最佳答案
你的代码完全坏了。看hgt数据的例子:
from netCDF4 import Dataset
import scipy.interpolate
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
nc_f = 'hgt_500.nc'
nc_fid = Dataset(nc_f, 'r')
lats = nc_fid.variables['lat']
lons = nc_fid.variables['lon']
time = nc_fid.variables['time']
hgt = nc_fid.variables['hgt']
m = Basemap(width=5000000,height=3500000,
resolution='l',projection='stere', lat_0 = 60, lon_0 = 70, lat_ts = 40)
m.drawcoastlines()
m.drawcountries()
lons, lats = np.meshgrid(lons, lats)
x, y = m(lons, lats)
# plot the first ZZ of hgt500
clevs = np.arange(400.,604.,4.)
cs = m.contour(x, y, hgt[0] * .1, clevs, linewidths=1.5, colors = 'k')
plt.clabel(cs, inline=1, fontsize=15, color='k', fmt='%.0f')
# color grid
pcl = m.pcolor(x,y,np.squeeze(hgt[0]*.1))
cbar = m.colorbar(pcl, location='bottom', pad="10%")
cbar.set_label("hPa")
plt.title('500 hPa Geopotential Height')
plt.show()
结果:
关于python - Matplotlib - 如何用海岸线和国家叠加等高线图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38048418/