简体   繁体   English

带有matplotlib的netCDF数据的怪异图

[英]Weird plot for netCDF data with matplotlib

I've been searching for a pathetically long time for this, so I would appreciate any help or hint I can get. 我一直在寻找可悲的长时间,因此,我将不胜感激。

I'm trying to plot some sea ice freeboard data (netCDF, Gridded total freeboard) on the Antarctic sea, but the data that should plot nicely around Antarctica lies at the bottom of my image. 我正在尝试在南极海中绘制一些海冰干舷数据(netCDF,网格总干舷),但是应该很好地绘制在南极周围的数据位于图像的底部。 NetCDF and matplotlib are fairly new to me so maybe the error could be eg with handling the dimensions or the projection. 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 gives: 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" ;

One weird thing I noticed is that min lat is -5156.6201 but I didn't know how to count how many of them there are... 我注意到的一件奇怪的事是最小纬度为-5156.6201,但我不知道如何计算其中的多少...

Edit: Formated the code to fit the common way, like Neil advised. 编辑:如尼尔建议的那样,将代码格式化为适合通用的方式。

Okay, I got help from matplotlib and thought I should share this here if someone else has sometimes similar problems. 好的,我从matplotlib获得帮助,并认为如果其他人有时也遇到类似的问题,我应该在这里分享。 The problem was with meshgrid. 问题出在网格上。 Since the latitudes and longitudes in the netCDF file were already in 2D the meshgrid was unnecessary. 由于netCDF文件中的纬度和经度已经是2D格式,因此不需要网格。 The solution that worked for me was: 对我有用的解决方案是:

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()

First, it's common practice to read in the netcdf module as 首先,通常的做法是将netcdf模块读为

from scipy.io.netcdf import netcdf_file as Dataset

You can then read in the file and access variables as 然后,您可以读取文件并以以下方式访问变量

FB = Dataset('./datasets/fb-0217-0320.nc', 'r')
f = FB.variables['f'][:,:]
lat = FB.variables['lat'][:,:]
lon = FB.variables['lon'][:,:]

Are you sure that lat[:,0] and lon[0,:] is reading in the grid coordinates correctly? 您确定lat[:,0]lon[0,:]在网格坐标中正确读取吗? ncdump indicates they are 2D variables and I suspect that the issue is creating a meshgrid from lat[:,0] and lon[0,:] . ncdump表示它们是2D变量,我怀疑问题是根据lat[:,0]lon[0,:]创建了一个网meshgrid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM