我一直在为此寻找可怜的时间,所以我将不胜感激任何帮助或暗示。
我试图在南极海上绘制一些海冰干舷数据(netCDF,网格化总干舷),但应该在南极洲周围很好地绘制的数据位于我的图像底部。 NetCDF和matplotlib对我来说相当新,所以错误可能是例如处理尺寸或投影。
from scipy.io.netcdf import netcdf_file as Dataset
import numpy as np
import matplotlib.pyplot as plt
FB = Dataset('./datasets/fb-0217-0320.nc', 'r')
f = FB.variables['f'][:,:]
lat = FB.variables['lat'][:,0]
lon = FB.variables['lon'][0,:]
masked_fb = np.ma.masked_where(np.isnan(f), f)
mtx_lon, mtx_lat = np.meshgrid(lon, lat)
m = Basemap(projection='spstere',boundinglat=-50, lon_0=180., resolution='l')
m.bluemarble()
plt.figure()
m.pcolormesh(mtx_lon, mtx_lat, masked_fb, latlon=True)
plt.show()
ncdump给出:
dimensions:
x = 79 ;
y = 83 ;
variables:
float lat(y, x) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude coordinate" ;
lat:units = "degrees_north" ;
float lon(y, x) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude coordinate" ;
lon:units = "degrees_east" ;
float f(y, x) ;
f:long_name = "total_freeboard" ;
f:units = "mm" ;
f:coordinates = "lat lon" ;
我注意到一个奇怪的事情是min lat是-5156.6201,但我不知道如何计算它们中有多少......
修改:像Neil建议的那样,将代码格式化为常用方式。
答案 0 :(得分:1)
好的,我从matplotlib得到了帮助,并且如果其他人有时遇到类似的问题,我认为我应该在这里分享一下。问题出在meshgrid上。由于netCDF文件中的纬度和经度已经在2D中,因此不需要网格网格。对我有用的解决方案是:
from scipy.io.netcdf import netcdf_file as Dataset
import numpy as np
import matplotlib.pyplot as plt
FB = Dataset('./datasets/fb-0217-0320.nc', 'r')
f = FB.variables['f'][:,:]
lat = FB.variables['lat'][:,:]
lon = FB.variables['lon'][:,:]
masked_fb = np.ma.masked_where(np.isnan(f), f)
m = Basemap(projection='spstere',boundinglat=-50, lon_0=180., resolution='l')
m.bluemarble()
plt.figure()
m.pcolormesh(lon, lat, masked_fb, latlon=True)
plt.show()
答案 1 :(得分:0)
首先,通常的做法是将netcdf模块读入
from scipy.io.netcdf import netcdf_file as Dataset
然后,您可以读入文件并访问变量
FB = Dataset('./datasets/fb-0217-0320.nc', 'r')
f = FB.variables['f'][:,:]
lat = FB.variables['lat'][:,:]
lon = FB.variables['lon'][:,:]
您确定lat[:,0]
和lon[0,:]
正确读取网格坐标吗? ncdump表示它们是2D变量,我怀疑问题是从meshgrid
和lat[:,0]
创建lon[0,:]
。