这是我第一次尝试使用 matplotlib 进行绘图。我正在尝试使用 matplotlib 绘制位势高度的 netCDF 文件数据,但得到一个空白图像。没有海岸线或任何东西。只是一个空白屏幕。 我的数据范围从 5 N 到 40 N,从 65 E 到 100 E。
我打印出了纬度、经度和纬度的值,它们都是有效的。
这是我的代码 -
nc_f = './hgt_500_2014_12_5_00Z.nc' # Your filename
nc_fid = Dataset(nc_f, 'r') # Dataset is the class behavior to open the file
lats = nc_fid.variables['lat'][:]
lons = nc_fid.variables['lon'][:]
time = nc_fid.variables['time'][:]
hgt = nc_fid.variables['hgt'][:]
hgt_units = nc_fid.variables['hgt'].units
nc_fid.close()
lon_0 = lons.mean()
lat_0 = lats.mean()
m = Basemap(width=5000000,height=3500000,
resolution='l',projection='stere',\
lat_ts=40,lat_0=lat_0,lon_0=lon_0)
lon, lat = np.meshgrid(lons, lats)
xi, yi = m(lon, lat)
cs = m.pcolor(xi,yi,np.squeeze(hgt))
m.drawparallels(np.arange(5., 40., 5.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(65., 100., 5.), labels=[0,0,0,1], fontsize=10)
m.drawcoastlines()
m.drawstates()
m.drawcountries()
cbar = m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(hgt_units)
plt.title('Geopotential Height')
plt.show()
当我运行 python 时,我也遇到这个错误
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/matplotlib-1.5.0-py3.4-linux-x86_64.egg/matplotlib/backends/backend_gtk3agg.py", line 69, in on_draw_event
buf, cairo.FORMAT_ARGB32, width, height)
NotImplementedError: Surface.create_for_data: Not Implemented yet.
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/matplotlib-1.5.0-py3.4-linux-x86_64.egg/matplotlib/backends/backend_gtk3agg.py", line 69, in on_draw_event
buf, cairo.FORMAT_ARGB32, width, height)
NotImplementedError: Surface.create_for_data: Not Implemented yet.
最佳答案
您可以像
lons = nc_fid.variables['lon']
那样编写。检查您的经纬度值,它们必须覆盖您所在区域的北纬 5 到 40 度以及东经 65 到 100 度的区域。
drawstates()
不适用于您所在的地区,此命令绘制美国的边界。检查其他 GUI 后端:TkAgg、WX、QTAgg、QT4Agg,例如:
导入matplotlib
matplotlib.use('Agg')
开罗库可能有问题
关于python - 无法使用matplotlib显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38011334/