diff --git a/glidertest/plots.py b/glidertest/plots.py index 93a94de..db0d9ff 100644 --- a/glidertest/plots.py +++ b/glidertest/plots.py @@ -308,6 +308,8 @@ def plot_quench_assess(ds: xr.Dataset, sel_var: str, ax: plt.Axes = None, start_ with plt.style.context(glidertest_style_file): if ax is None: fig, ax = plt.subplots() + full_width = fig.get_size_inches()[0] + fig.set_size_inches(full_width, full_width * 0.5) else: fig = plt.gcf() @@ -325,6 +327,9 @@ def plot_quench_assess(ds: xr.Dataset, sel_var: str, ax: plt.Axes = None, start_ ds_sel = ds.sel(TIME=slice(t1,t2)) else: ds_sel = ds.sel(TIME=slice(start_time, end_time)) + start_time = ds_sel.TIME.min().values + end_time = ds_sel.TIME.max().values + if len(ds_sel.TIME) == 0: msg = f"supplied limits start_time: {start_time} end_time: {end_time} do not overlap with dataset TIME range {str(ds.TIME.values.min())[:10]} - {str(ds.TIME.values.max())[:10]}" @@ -340,6 +345,20 @@ def plot_quench_assess(ds: xr.Dataset, sel_var: str, ax: plt.Axes = None, start_ for m in np.unique(sunrise): ax.axvline(np.unique(m), c='orange') ax.set_ylabel('Depth [m]') + + # Set x-tick labels based on duration of the selection + # Could pop out as a utility plotting function? + if (end_time - start_time) < np.timedelta64(1, 'D'): + ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) + start_date = pd.to_datetime(start_time).strftime('%Y-%b-%d') + end_date = pd.to_datetime(end_time).strftime('%Y-%b-%d') + if start_date==end_date: + ax.set_xlabel(f'Time [UTC] ({start_date})') + else: + ax.set_xlabel(f'Time [UTC] ({start_date} to {end_date})') + else: + ax.xaxis.set_major_formatter(DateFormatter('%d-%b')) + plt.colorbar(c, label=f'{sel_var} [{ds[sel_var].units}]') plt.show() return fig, ax diff --git a/notebooks/demo.ipynb b/notebooks/demo.ipynb index 1de7370..9d349a6 100644 --- a/notebooks/demo.ipynb +++ b/notebooks/demo.ipynb @@ -302,9 +302,7 @@ "outputs": [], "source": [ "# Let's visually check a section of chlorphyll and see if we observe any NPQ\n", - "fig, ax = plt.subplots(1, 1, figsize=(15, 5))\n", - "\n", - "plots.plot_quench_assess(ds, 'CHLA', ax, ylim=35);" + "plots.plot_quench_assess(ds, 'CHLA', ax=None, ylim=35);" ] }, { @@ -478,7 +476,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -492,7 +490,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.12.7" } }, "nbformat": 4,