From b6704663298d63a50608291ad8e8763f5e94d75f Mon Sep 17 00:00:00 2001 From: dbrakenhoff Date: Mon, 5 Jun 2023 16:29:42 +0200 Subject: [PATCH 1/2] use xarray facet plot in nb --- docs/examples/00_model_from_scratch.ipynb | 28 +++++++++------ nlmod/plot/flopy.py | 8 ----- nlmod/plot/plot.py | 44 ++++++++++++----------- 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/docs/examples/00_model_from_scratch.ipynb b/docs/examples/00_model_from_scratch.ipynb index b3a5f8ee..3325e06f 100644 --- a/docs/examples/00_model_from_scratch.ipynb +++ b/docs/examples/00_model_from_scratch.ipynb @@ -277,8 +277,8 @@ " ds=ds,\n", " cmap=\"RdYlBu\",\n", " ax=ax,\n", - " edgecolor=\"k\",\n", ")\n", + "nlmod.plot.modelgrid(ds, ax=ax, alpha=0.5, lw=0.5)\n", "ax.axis(extent)\n", "cbar = ax.figure.colorbar(pc, shrink=1.0)\n", "cbar.set_label(\"head [m NAP]\")\n", @@ -300,17 +300,25 @@ "metadata": {}, "outputs": [], "source": [ - "fig, axes = nlmod.plot.facet_plot(\n", - " gwf,\n", - " head,\n", - " lbl=\"head [m NAP]\",\n", - " plot_dim=\"layer\",\n", - " period=0,\n", + "fg = head.plot(\n", + " x=\"x\",\n", + " y=\"y\",\n", + " col=\"layer\",\n", + " col_wrap=3,\n", " cmap=\"RdYlBu\",\n", - " plot_bc={\"WEL\": {}},\n", - " plot_grid=True,\n", - ")" + " subplot_kws={\"aspect\": \"equal\"},\n", + ")\n", + "\n", + "for iax in fg.axs.flat:\n", + " nlmod.plot.modelgrid(ds, ax=iax, alpha=0.5, lw=0.5)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/nlmod/plot/flopy.py b/nlmod/plot/flopy.py index 0e2d7e0b..58ced313 100644 --- a/nlmod/plot/flopy.py +++ b/nlmod/plot/flopy.py @@ -235,7 +235,6 @@ def facet_plot( xlim=None, ylim=None, grid=False, - figdir=None, figsize=(10, 8), plot_bc=None, plot_grid=False, @@ -317,11 +316,4 @@ def facet_plot( cb = fig.colorbar(qm, ax=axes, shrink=1.0) cb.set_label(lbl) - if figdir: - fig.savefig( - os.path.join(figdir, f"{lbl}_per_{plot_dim}.png"), - dpi=150, - bbox_inches="tight", - ) - return fig, axes diff --git a/nlmod/plot/plot.py b/nlmod/plot/plot.py index b7a6005e..867404a4 100644 --- a/nlmod/plot/plot.py +++ b/nlmod/plot/plot.py @@ -1,4 +1,4 @@ -import os +import warnings from functools import partial import flopy as fp @@ -46,10 +46,9 @@ def modelgrid(ds, ax=None, **kwargs): def facet_plot( gwf, ds, - figdir, - plot_var="bot", + plot_var, plot_time=None, - plot_bc=("CHD",), + plot_bc=None, color="k", grid=False, xlim=None, @@ -65,8 +64,8 @@ def facet_plot( model dataset. figdir : str file path figures. - plot_var : str, optional - variable in ds. The default is 'bot'. + plot_var : str + variable in ds plot_time : int, optional time step if plot_var is time variant. The default is None. plot_bc : list of str, optional @@ -88,12 +87,19 @@ def facet_plot( axes : TYPE DESCRIPTION. """ - for key in plot_bc: - if key not in gwf.get_package_list(): - raise ValueError( - f"cannot plot boundary condition {key} " - "because it is not in the package list" - ) + + warnings.warn( + "this function is out of date and will probably be removed in a future version", + DeprecationWarning, + ) + + if plot_bc is not None: + for key in plot_bc: + if key not in gwf.get_package_list(): + raise ValueError( + f"cannot plot boundary condition {key} " + "because it is not in the package list" + ) nlay = len(ds.layer) @@ -116,15 +122,16 @@ def facet_plot( vmin = plot_arr.min() vmax = plot_arr.max() for ilay in range(nlay): - iax = axes.ravel()[ilay] + iax = axes.flat[ilay] mp = fp.plot.PlotMapView(model=gwf, layer=ilay, ax=iax) # mp.plot_grid() qm = mp.plot_array(plot_arr[ilay].values, cmap="viridis", vmin=vmin, vmax=vmax) # qm = mp.plot_array(hf[-1], cmap="viridis", vmin=-0.1, vmax=0.1) # mp.plot_ibound() # plt.colorbar(qm) - for bc_var in plot_bc: - mp.plot_bc(bc_var, kper=0, color=color) + if plot_bc is not None: + for bc_var in plot_bc: + mp.plot_bc(bc_var, color=color, kper=0) iax.set_aspect("equal", adjustable="box") iax.set_title(f"Layer {ilay}") @@ -135,18 +142,13 @@ def facet_plot( if ylim is not None: iax.set_ylim(ylim) - for iax in axes.ravel()[nlay:]: + for iax in axes.flat[nlay:]: iax.set_visible(False) cb = fig.colorbar(qm, ax=axes, shrink=1.0) cb.set_label(f"{plot_var}", rotation=270) fig.suptitle(f"{plot_var} Time = {(ds.nper*ds.perlen)/365} year") fig.tight_layout() - fig.savefig( - os.path.join(figdir, f"{plot_var}_per_layer.png"), - dpi=150, - bbox_inches="tight", - ) return fig, axes From 91fd6ce6ddd4c38d59016ecd62f7d922d2311f28 Mon Sep 17 00:00:00 2001 From: dbrakenhoff Date: Mon, 5 Jun 2023 16:29:52 +0200 Subject: [PATCH 2/2] use xarray facet plot in nb --- docs/examples/run_all_notebooks.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/examples/run_all_notebooks.py b/docs/examples/run_all_notebooks.py index fc619b35..86f5101e 100644 --- a/docs/examples/run_all_notebooks.py +++ b/docs/examples/run_all_notebooks.py @@ -1,3 +1,4 @@ +# %% from glob import glob import nbformat from nbconvert.preprocessors import ExecutePreprocessor @@ -44,3 +45,5 @@ os.system( f"jupyter nbconvert --clear-output --inplace --ClearMetadataPreprocessor.enabled=True {notebook}" ) + +# %%