如何绘制具有正确轴格式的 NetCDF 时间相关数据集?

How to plot a NetCDF time dependend data set with correct axis format?

我根据 NetCDF 数据格式的数据制作了 pcolormesh-plot。我不管理 x 轴和 y 轴来显示数据集中的右轴刻度。相反,两个轴都从零开始并以点数结束。 从 NetCDF 描述 https://unidata.github.io/netcdf4-python/ 我也不明白。

有什么建议吗?

这是我所做的:

我加载的包:

import netCDF4 as nc
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm, SymLogNorm, Normalize

from pylab import cm as pylab_cm

from bokeh.plotting import figure, show, output_file, output_notebook, ColumnDataSource
from bokeh.io import output_notebook, export_png
from bokeh.models.tools import HoverTool, BoxZoomTool, ResetTool, SaveTool, WheelZoomTool, ZoomOutTool
from bokeh.models.tools import PanTool, UndoTool, RedoTool, BoxEditTool, FreehandDrawTool, PolyDrawTool, PolyEditTool
from bokeh.layouts import column, gridplot
from bokeh.models import Band, LinearAxis, Range1d, Label, LabelSet, Title

我的(多维)变量的数据格式是:

print(ds['raw'])

给出结果:

<class 'netCDF4._netCDF4.Variable'>
float32 raw(time, range)
    units: 
    long_name: normalized range corrected signal
unlimited dimensions: time
current shape = (5760, 1024)
filling on, default _FillValue of 9.969209968386869e+36 used

我加载数据集并转置它,因为我想要 x 轴上的时间戳:

nrcs = ds['raw'][:]
nrcs = nrcs.transpose() 

由于“float32 raw(time, range)”,这应该已经包含了时间信息,对吗?

我重新格式化时间戳:

dtime = nc.num2date(ds['time'][:], ds['time'].units)

然后我创建我的情节:

fig1 = plt.figure(figsize=(14,8))
ax = fig1.add_subplot(1,1,1)
im = ax.pcolormesh(nrcs, norm=Normalize(vmin=t[8], vmax=t[11], clip=False), cmap=pylab_cm.viridis_r, shading='gouraud')

# I tried this but it does not work:
#ax.xaxis_date()
#ax.xaxis.set_major_formatter(mdates.DateFormatter('%H %M'))

ax.set_xlabel(r'Time / hh:mm:ss', fontsize=10)
ax.set_ylabel(r'Altitude', fontsize=10)

结果如下所示:

实际上,y轴上的高度数据是从0...到10 000 m以上,x轴应该是时间格式。有什么提示可以让 matlibplot 读取正确的尺寸吗?

提前致谢。

同时我使用包“xarray”读取数据集

import xarray as xr
dataDIR = 'cdata.nc'
DS = xr.open_dataset(dataDIR)

绘制数据的简单方法是使用pandas“绘图”。

DS.b_r.plot()

使用散景更便于用户探索数据。为此,“hvplot”-package 有助于绘制 xarray netCDF-Data,一些 holoview 导入有助于事后自定义绘图:

import hvplot.xarray
import holoviews as hv
from holoviews import dim, opts

-当然-bokeh-imports 是必需的:

from bokeh.plotting import figure, show, output_file, output_notebook, ColumnDataSource
from bokeh.io import output_notebook, export_png
from bokeh.models.tools import HoverTool, BoxZoomTool, ResetTool, SaveTool, WheelZoomTool, ZoomOutTool
from bokeh.models.tools import PanTool, UndoTool, RedoTool, BoxEditTool, FreehandDrawTool, PolyDrawTool, PolyEditTool
from bokeh.layouts import column, gridplot
from bokeh.models import Band, LinearAxis, Range1d, Label, LabelSet, Title

绘制数据:

DS.b_r.hvplot(cmap="viridis_r",
                   cbar_kwargs = {"orientation": "vertical", "shrink": 0.8, "aspect": 40,
                                "label": "Backscatter Signal"},
                   clim = (0, 500),  # adds a widget for time  # sets colorbar limits
                   clabel = ('Numbers') # adds a title to the colorbar
                   title = 'Title', 
                   width = 850, # plot width
                   height = 600, # plot height
                   rot = 0  # degree rotation of ticks
                   )

现在,使用 NetCDF 可视化数据可以轻松工作:-)

更多文档: