代码之家  ›  专栏  ›  技术社区  ›  Chiel

用xarray打印组中的NetCDF变量时缺少轴值

  •  1
  • Chiel  · 技术社区  · 7 年前

    我正在构建一个NetCDF文件,它将与 xarray . 它将由许多使用在根组中定义的维度的组组成。在我现在的例子中, plot panoply ncview 生成在轴上放置正确尺寸值的绘图。下面的脚本创建了一个文件,允许我重现问题。我是否以错误的方式构造NetCDF文件,或者这是一个错误 ?

    import numpy as np
    import netCDF4 as nc
    import xarray as xr
    import matplotlib.pyplot as plt
    
    # Three series, two variables that contain the axis values and the 2D field.
    z = np.arange(0., 1000., 50.)
    time = np.arange(0., 86400., 3600.)
    a = np.random.rand(time.size, z.size)
    
    # The two dimensions are stored in the root group.
    nc_file = nc.Dataset("test.nc", mode="w", datamodel="NETCDF4", clobber=False)
    nc_file.createDimension("z"   , z.size   )
    nc_file.createDimension("time", time.size)
    
    nc_z    = nc_file.createVariable("z"   , "f8", ("z")   )
    nc_time = nc_file.createVariable("time", "f8", ("time"))
    nc_z   [:] = z   [:]
    nc_time[:] = time[:]
    
    # The 2D field is created and stored in a group called test_group.
    nc_group = nc_file.createGroup("test_group")
    
    nc_a = nc_group.createVariable("a", "f8", ("time", "z"))
    nc_a[:,:] = a[:,:]
    
    nc_file.close()
    
    # Opening the file in x-array shows a plot that misses the axes values.
    xr_file = xr.open_dataset("test.nc", "test_group")
    xr_a = xr_file['a']
    xr_a.plot()
    plt.show()
    

    生成的图形(轴上只有计数而不是尺寸值)为: Resulting plot

    0 回复  |  直到 7 年前