Visualization#

As the return value of a sliced dataset is a xarray.DataArray instead of a numpy.ndarray plotting features of xarray is used. For more information about xarray see https://docs.xarray.dev/en/stable/

import h5rdmtoolbox as h5tbx
h5tbx.use(None)

import matplotlib.pyplot as plt
import numpy as np
Failed to import module h5tbx
with h5tbx.File() as h5:
    dsx = h5.create_dataset('x', data=np.linspace(0, 10, 20), attrs=dict(units='mm', long_name='x'), make_scale=True)
    dsy = h5.create_dataset('y', data=np.linspace(0, 5, 10), attrs=dict(units='mm', long_name='y'), make_scale=True)
    dsz = h5.create_dataset('z', data=np.linspace(0, 3, 4), attrs=dict(units='mm', long_name='z'), make_scale=True)
    h5.create_dataset('data', data=np.random.random((10, 20)), attrs=dict(units='m/s', long_name='velocity'), attach_scales=(dsy, dsx))
    
    xx, yy, zz = np.meshgrid(dsy.values[:], dsz.values[:], dsx.values[:])
    
    h5.create_dataset('u', data=np.sin(xx), attrs=dict(units='m/s', long_name='x_velocity'), attach_scales=(dsz, dsy, dsx))
    h5.create_dataset('v', data=yy, attrs=dict(units='m/s', long_name='y_velocity'), attach_scales=(dsz, dsy, dsx))
    h5.create_dataset('w', data=np.ones((4, 10, 20)), attrs=dict(units='m/s', long_name='z_velocity'), attach_scales=(dsz, dsy, dsx))
    h5.dump()

Xarray plots#

xarray comes with a lot of useful plotting features:

Line plots#

with h5tbx.File(h5.hdf_filename) as h5:
    h5.dump()
    d = h5['data'][:, 0]
    d.plot.line(marker='o')
../../_images/913cf20ca055ebb4d663c92d67d15c73856bfb29d2fcd6c0a0e96e05971c007c.png

2D plots#

with h5tbx.File(h5.hdf_filename) as h5:
    # some plotting
    plt.figure()
    h5['data'][:].plot()
    plt.show()
    
    plt.figure()
    h5['data'][:].plot.contourf()
    plt.figure()
    plt.show()
../../_images/438a2d7f5a8c5d010669589c25fef9b3d514745350447aef01d6a617051cca57.png ../../_images/e3bbc70fdaa569ce09e28908813a2ce873d78460cd5c0bec8d21e35077604ed6.png
<Figure size 640x480 with 0 Axes>

Histograms#

with h5tbx.File(h5.hdf_filename) as h5:
    h5['data'][:].plot.hist()
    plt.show()
../../_images/4ca0274e7dd7450c6406ff00d1011fcf36a9edb3f80db13e75b6e05ef5821d5d.png

Vector plotting with h5rdmtoolbox and xarray#

The toolbox provides a Vector accessory (see also section “Extensions”), which constructs a xarray dataset:

from h5rdmtoolbox.extensions import vector
with h5tbx.File(h5.hdf_filename) as h5:
    ds = h5.Vector(u=h5.u, v=h5.v)
    ds[2, :, :].plot.quiver(x='x', y='y', u='u', v='v')
../../_images/f3ececd5da0d0633957bb24233cd597bd7c24bc114709915f518ad710ebd0ab2.png