python - Matplotlib - 如何用海岸线和国家叠加等高线图

标签 python matplotlib python-3.4 matplotlib-basemap

我有这个 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() 

结果: enter image description here

关于python - Matplotlib - 如何用海岸线和国家叠加等高线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38048418/

相关文章:

python - 过滤登录用户创建的预订

python - 我可以将 Python 脚本作为 Glib GModules 加载吗?

python - 在 Pyspark 中读取和保存图像文件

python - 使用 Mayavi 和 Mlab 绘制 "bubbles"

mongodb - pymongo 无法检索文件

python - 当轴点是 bin 时,如何在 matplotlib 中对轴进行排序?

python - 椭圆没有显示 - python matplotlib

python - 从元组中的一组点制作散点图

python - 添加到购物篮,并从库存中取出等值产品

python - 为什么我的python程序总是显示相反的 react