"""Transport time-series plotting."""
from pathlib import Path
from typing import Any
import matplotlib.pyplot as plt
import xarray as xr
[docs]
def plot_monthly_transport(
ds: xr.Dataset, var: str = "moc_mar_hc10"
) -> tuple[Any, Any]:
"""Plot original and monthly averaged transport time series.
Parameters
----------
ds : xr.Dataset
Dataset with a time dimension and a transport variable.
var : str, optional
Name of the variable to plot. Default is "moc_mar_hc10".
"""
# mplstyle lives at the package root (template_project/template_project.mplstyle)
package_root = Path(__file__).resolve().parent.parent
plt.style.use(package_root / "template_project.mplstyle")
da = ds[var]
ds_monthly = ds.resample(TIME="ME").mean()
fig, ax = plt.subplots()
ax.plot(ds.TIME, da, color="grey", alpha=0.5, linewidth=0.5, label="Original")
ax.plot(
ds_monthly.TIME,
ds_monthly[var],
color="red",
linewidth=1.0,
label="Monthly Avg",
)
ax.axhline(0, color="black", linestyle="--", linewidth=0.5)
ax.set_title("RAPID 26°N - AMOC")
# Use variable attributes if present
label = da.attrs.get("long_name", var)
units = da.attrs.get("units", "")
ax.set_ylabel(f"{label} [{units}]" if units else label)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.legend()
plt.tight_layout()
return fig, ax