Matplotlib如何将等高线图与海岸线和国家叠加

2024-06-01 01:02:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我想把这片土地与海岸线和国家叠加起来。 我试过这样做,但这给了我海岸线和国家,但地球位高度等值线却不见了

^{pr2}$

Tags: 高度timepltvariableslon海岸线copylat
1条回答
网友
1楼 · 发布于 2024-06-01 01:02:20

你的密码完全坏了。看看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

相关问题 更多 >