python - 无法使用matplotlib显示数据

标签 python python-3.x matplotlib plot netcdf

这是我第一次尝试使用 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.

最佳答案

  1. 您可以像 lons = nc_fid.variables['lon'] 那样编写。

  2. 检查您的经纬度值,它们必须覆盖您所在区域的北纬 5 到 40 度以及东经 65 到 100 度的区域。

  3. drawstates() 不适用于您所在的地区,此命令绘制美国的边界。

  4. 检查其他 GUI 后端:TkAgg、WX、QTAgg、QT4Agg,例如:

    导入matplotlib

    matplotlib.use('Agg')

  5. 开罗库可能有问题

关于python - 无法使用matplotlib显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38011334/

相关文章:

python - 在 Python 中查找函数的根

python-3.x - 将每个组乘以 python groupby 中列表中的特定值

matplotlib - 更改 Matplotlib 中科学计数法刻度标签的大小

python - 如何在 Python 中拆分此字符串?

python - 使用多个迭代器同时遍历两个列表

python - 多字符串列映射和清理

python - 将 .txt 文件的名称放入列表中

image - 如果图像数据形状发生变化,则使用 imshow 设置正确的限制

python - 使用 matplotlib 在 Python 中绘制参数图

error-handling - 分配默认值的 Pythonic 方式