Plotting wave spectra data, using the accessor syntax#

from pathlib import Path
from trajan.readers.omb import read_omb_csv
import coloredlogs
import datetime
import matplotlib.pyplot as plt

# adjust the level of information printed
# coloredlogs.install(level='error')
coloredlogs.install(level='debug')
# load the data from an example file with several buoys and a bit of wave spectra data
path_to_test_data = Path.cwd().parent / "tests" / "test_data" / "csv" / "omb3.csv"
xr_data = read_omb_csv(path_to_test_data)
2024-12-16 16:27:17 fv-az1766-447 trajan.readers.omb[2121] DEBUG reading /home/runner/work/trajan/trajan/tests/test_data/csv/omb3.csv..
2024-12-16 16:27:17 fv-az1766-447 trajan.readers.omb[2121] DEBUG omb_dataframe at index 94 is:
Date Time (UTC)    16/Jun/2022 18:27:19
Device                        drifter_2
Direction                            MO
Payload                             NaN
Approx Lat/Lng          74.36745,3.3274
Payload (Text)                      NaN
Length (Bytes)                        0
Credits                               1
Name: 94, dtype: object
this is empty (Length (Bytes) is 0), drop
2024-12-16 16:27:17 fv-az1766-447 trajan.readers.omb[2121] DEBUG start applying sliding_filter_nsigma
2024-12-16 16:27:17 fv-az1766-447 trajan.readers.omb[2121] DEBUG done applying sliding_filter_nsigma
2024-12-16 16:27:17 fv-az1766-447 trajan.readers.omb[2121] DEBUG start applying sliding_filter_nsigma
2024-12-16 16:27:17 fv-az1766-447 trajan.readers.omb[2121] DEBUG done applying sliding_filter_nsigma
2024-12-16 16:27:17 fv-az1766-447 trajan.readers.omb[2121] DEBUG start applying sliding_filter_nsigma
2024-12-16 16:27:17 fv-az1766-447 trajan.readers.omb[2121] DEBUG done applying sliding_filter_nsigma
2024-12-16 16:27:17 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting trajectory dimension
2024-12-16 16:27:17 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting time-variable for "obs"..
2024-12-16 16:27:17 fv-az1766-447 trajan.accessor[2121] DEBUG Detected obs-dim: obs, detected time-variable: time.
2024-12-16 16:27:17 fv-az1766-447 trajan.accessor[2121] DEBUG Detected un-structured (2D) trajectory dataset
2024-12-16 16:27:17 fv-az1766-447 trajan.traj[2121] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2024-12-16 16:27:17 fv-az1766-447 trajan.traj[2121] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2024-12-16 16:27:17 fv-az1766-447 trajan.traj[2121] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2024-12-16 16:27:17 fv-az1766-447 trajan.traj[2121] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
# if no axis is provided, an axis will be generated automatically
# by default, we "decorate", i.e. label axis etc

xr_data.isel(trajectory=0).processed_elevation_energy_spectrum.wave.plot(
    xr_data.isel(trajectory=0).time_waves_imu.squeeze(),
    )

plt.show()
example plot spectra accessors
2024-12-16 16:27:17 fv-az1766-447 trajan.waves[2121] DEBUG Setting up new plot object.
2024-12-16 16:27:17 fv-az1766-447 matplotlib.colorbar[2121] DEBUG locator: <matplotlib.ticker.AutoLocator object at 0x7f0092426fd0>
# it is also possible to provide an axis on which to plot
# in this case, this will likely be part of a larger figure, and the user will likely want to put the
# labels etc themselves; remember to switch off decoration

# a plot with 3 lines, 2 columns
fig, ax = plt.subplots(3, 2)

ax_out, pclr = xr_data.isel(trajectory=0).processed_elevation_energy_spectrum.wave.plot(
    xr_data.isel(trajectory=0).time_waves_imu.squeeze(),
    # plot on the second line, first column
    ax=ax[1, 0],
    decorate=False,
    )

# the user can make the plot to their liking
ax[1, 0].set_xticks(ax[1, 0].get_xticks(), ax[1, 0].get_xticklabels(), rotation=45, ha='right')
ax[1,0].set_ylim([0.05, 0.25])
ax[1,0].set_ylabel("f [Hz]")

plt.tight_layout()

cbar = plt.colorbar(pclr, ax=ax, orientation='vertical', shrink=0.8)
cbar.set_label('log$_{10}$(S) [m$^2$/Hz]')

plt.show()
example plot spectra accessors
2024-12-16 16:27:17 fv-az1766-447 trajan.waves[2121] DEBUG Setting up new plot object.
/home/runner/work/trajan/trajan/examples/example_plot_spectra_accessors.py:58: MatplotlibDeprecationWarning: Getting the array from a PolyQuadMesh will return the full array in the future (uncompressed). To get this behavior now set the PolyQuadMesh with a 2D array .set_array(data2d).
  cbar = plt.colorbar(pclr, ax=ax, orientation='vertical', shrink=0.8)
2024-12-16 16:27:18 fv-az1766-447 matplotlib.colorbar[2121] DEBUG locator: <matplotlib.ticker.AutoLocator object at 0x7f007a723110>

Total running time of the script: (0 minutes 0.629 seconds)

Gallery generated by Sphinx-Gallery