'How to plot a NetCDF time dependend data set with correct axis format?
I made a pcolormesh-plot from data in NetCDF data format. I don't manage the x- and y-axis to show the right axis ticks from the data set. Instead both axis start from zero and end with the number of points. From the NetCDF desciption https://unidata.github.io/netcdf4-python/ I also do not get the point.
Any tip for me?
Here is what I did:
Packages I load:
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
Data format of my (more-dimensional) variable is:
print(ds['raw'])
gives as result:
<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
I load the data set and transpose it as I want the time stamp on the x-axis:
nrcs = ds['raw'][:]
nrcs = nrcs.transpose()
This should -due to 'float32 raw(time, range)- already contain the time information, right?
I reformat the time stamp:
dtime = nc.num2date(ds['time'][:], ds['time'].units)
Then I create my plot:
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)
And the result looks like this:
In real, the altitude data on the y-axis is from 0...to above 10 000 m and the x-axis should be a time format. Any hint how I can bring the matlibplot to read the right dimensions?
Thanks in advance.
Solution 1:[1]
In the mean time I use the package "xarray" to read in the dataset
import xarray as xr
dataDIR = 'cdata.nc'
DS = xr.open_dataset(dataDIR)
Simple way to plot data is using pandas "plot".
DS.b_r.plot()
More user friendly to explore data is using bokeh. Herefor "hvplot"-package is helpful to plot xarray netCDF-Data and some holoview imports help to customize the plot afterwards:
import hvplot.xarray
import holoviews as hv
from holoviews import dim, opts
and -of course- the bokeh-imports are neccessary:
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
Plotting data:
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
)
Now, working is easy with NetCDF visualized data :-)
For further documentation:
xarray Tutorials: https://xarray-contrib.github.io/xarray-tutorial/scipy-tutorial/04_plotting_and_visualization.html
xarray visualization gallery: https://xarray.pydata.org/en/stable/examples/visualization_gallery.html
Holoviz / hvplot: https://hvplot.holoviz.org/index.html
Holoview: custom plots with bokeh: https://holoviews.org/user_guide/Plotting_with_Bokeh.html[enter link description here]4
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |