Skip to content

Commit

Permalink
Merge pull request #244 from ArtesiaWater/plotting-in-notebooks
Browse files Browse the repository at this point in the history
improve plotting in notebooks #224
  • Loading branch information
dbrakenhoff authored Aug 25, 2023
2 parents 021fc6a + 4fa08c8 commit 8ecca8a
Show file tree
Hide file tree
Showing 26 changed files with 229 additions and 158 deletions.
31 changes: 18 additions & 13 deletions docs/examples/00_model_from_scratch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,17 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1, 1, figsize=(10, 8))\n",
"\n",
"pc = nlmod.plot.data_array(\n",
"# using nlmod plotting methods\n",
"ax = nlmod.plot.map_array(\n",
" head.sel(layer=0).isel(time=0),\n",
" ds=ds,\n",
" ds,\n",
" cmap=\"RdYlBu\",\n",
" ax=ax,\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",
"ax.set_title(\"head first layer\")\n",
"fig.tight_layout()"
" colorbar_label=\"head [m NAP]\",\n",
" xlabel=\"x [km]\",\n",
" ylabel=\"y [km]\",\n",
" title=\"head first layer\",\n",
" plot_grid=True,\n",
")"
]
},
{
Expand All @@ -300,13 +297,15 @@
"metadata": {},
"outputs": [],
"source": [
"# using xarray plotting methods\n",
"fg = head.plot(\n",
" x=\"x\",\n",
" y=\"y\",\n",
" col=\"layer\",\n",
" col_wrap=3,\n",
" cmap=\"RdYlBu\",\n",
" subplot_kws={\"aspect\": \"equal\"},\n",
" cbar_kwargs={\"label\": \"head [m NAP]\"},\n",
")\n",
"\n",
"for iax in fg.axs.flat:\n",
Expand All @@ -322,8 +321,14 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "artesia",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"name": "python",
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
28 changes: 17 additions & 11 deletions docs/examples/01_basic_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -262,30 +262,36 @@
"metadata": {},
"outputs": [],
"source": [
"fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(14, 11))\n",
"fig, axes = nlmod.plot.get_map(ds.extent, nrows=2, ncols=2, figsize=14)\n",
"ds[\"ahn\"].plot(ax=axes[0][0])\n",
"ds[\"botm\"][0].plot(ax=axes[0][1])\n",
"ds[\"idomain\"][0].plot(ax=axes[1][0])\n",
"ds[\"edge_mask\"][0].plot(ax=axes[1][1])\n",
"for axes1 in axes:\n",
" for ax in axes1:\n",
" ax.axis(\"scaled\")\n",
"\n",
"fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(14, 11))\n",
"fig, axes = nlmod.plot.get_map(ds.extent, nrows=2, ncols=2, figsize=14)\n",
"ds[\"bathymetry\"].plot(ax=axes[0][0])\n",
"ds[\"northsea\"].plot(ax=axes[0][1])\n",
"ds[\"kh\"][1].plot(ax=axes[1][0])\n",
"ds[\"recharge\"].plot(ax=axes[1][1])\n",
"\n",
"for axes1 in axes:\n",
" for ax in axes1:\n",
" ax.axis(\"scaled\")\n"
"ds[\"recharge\"].plot(ax=axes[1][1]);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "artesia",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"name": "python",
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
78 changes: 47 additions & 31 deletions docs/examples/02_surface_water.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,8 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1, 1, figsize=(10, 8))\n",
"ax.set_aspect(\"equal\", adjustable=\"box\")\n",
"sfw.plot(ax=ax, column=\"stage\", legend=True)\n",
"ax.grid(True)\n",
"ax.set_xlabel(\"X (m RD)\")\n",
"ax.set_ylabel(\"Y (m RD)\")\n",
"plt.yticks(rotation=90, va=\"center\")\n",
"fig.tight_layout()"
"fig, ax = nlmod.plot.get_map(extent)\n",
"sfw.plot(ax=ax, column=\"stage\", legend=True)"
]
},
{
Expand Down Expand Up @@ -446,14 +440,9 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1, 1, figsize=(10, 8))\n",
"ax.set_aspect(\"equal\", adjustable=\"box\")\n",
"fig, ax = nlmod.plot.get_map(extent)\n",
"sfw_grid.plot(ax=ax, column=\"cellid\")\n",
"gwf.modelgrid.plot(ax=ax, linewidth=0.5, color=\"k\")\n",
"xmin, xmax, ymin, ymax = extent\n",
"offset = 100\n",
"ax.set_xlim(xmin - offset, xmax + offset)\n",
"ax.set_ylim(ymin - offset, ymax + offset)\n"
"nlmod.plot.modelgrid(ds, ax=ax, lw=0.2)"
]
},
{
Expand Down Expand Up @@ -494,7 +483,10 @@
"source": [
"fig, ax = plt.subplots(1, 1, figsize=(10, 8))\n",
"sfw_grid.loc[mask].plot(\n",
" column=\"identificatie\", legend=True, ax=ax, legend_kwds={\"loc\": \"upper left\"}\n",
" column=\"identificatie\",\n",
" legend=True,\n",
" ax=ax,\n",
" legend_kwds={\"loc\": \"lower left\", \"ncol\": 2, \"fontsize\": \"x-small\"},\n",
")\n",
"xlim = ax.get_xlim()\n",
"ylim = ax.get_ylim()\n",
Expand Down Expand Up @@ -691,6 +683,7 @@
"metadata": {},
"outputs": [],
"source": [
"# use flopy plotting methods\n",
"fig, ax = plt.subplots(1, 1, figsize=(10, 8), constrained_layout=True)\n",
"mv = flopy.plot.PlotMapView(model=gwf, ax=ax, layer=0)\n",
"mv.plot_bc(\"RIV\")"
Expand Down Expand Up @@ -751,13 +744,17 @@
"metadata": {},
"outputs": [],
"source": [
"ilay = 0\n",
"fig, ax = plt.subplots(1, 1, figsize=(10, 8), constrained_layout=True)\n",
"mv = flopy.plot.PlotMapView(model=gwf, ax=ax, layer=ilay)\n",
"qm = mv.plot_array(head[-1], cmap=\"RdBu\") # last timestep\n",
"mv.plot_ibound() # plot inactive cells in red\n",
"fig.colorbar(qm, shrink=1.0)\n",
"ax.set_title(f\"Heads top-view, layer {ilay}\")\n"
"# using nlmod plotting methods\n",
"ax = nlmod.plot.map_array(\n",
" head,\n",
" ds,\n",
" ilay=0,\n",
" iper=0,\n",
" plot_grid=True,\n",
" title=\"Heads top-view\",\n",
" cmap=\"RdBu\",\n",
" colorbar_label=\"head [m NAP]\",\n",
")"
]
},
{
Expand All @@ -775,21 +772,40 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1, 1, figsize=(10, 3), constrained_layout=True)\n",
"xs = flopy.plot.PlotCrossSection(\n",
" model=gwf, ax=ax, line={\"column\": gwf.modelgrid.ncol // 2}\n",
")\n",
"# using flopy plotting methods\n",
"col = gwf.modelgrid.ncol // 2\n",
"\n",
"fig, ax = plt.subplots(1, 1, figsize=(10, 3))\n",
"xs = flopy.plot.PlotCrossSection(model=gwf, ax=ax, line={\"column\": col})\n",
"qm = xs.plot_array(head[-1], cmap=\"RdBu\") # last timestep\n",
"xs.plot_ibound() # plot inactive cells in red\n",
"fig.colorbar(qm, shrink=1.0)\n",
"col = gwf.modelgrid.ncol // 2\n",
"ax.set_title(f\"Cross-section along column {col}\")\n"
"xs.plot_grid(lw=0.25, color=\"k\")\n",
"ax.set_ylim(bottom=-150)\n",
"ax.set_ylabel(\"elevation [m NAP]\")\n",
"ax.set_xlabel(\"distance along cross-section [m]\")\n",
"ax.set_title(f\"Cross-section along column {col}\")\n",
"cbar = fig.colorbar(qm, shrink=1.0)\n",
"cbar.set_label(\"head [m NAP]\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e5833d6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "artesia",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"name": "python",
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/03_local_grid_refinement.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
"nlmod.plot.data_array(ds[\"bathymetry\"], ds, ax=axes[0][0])\n",
"nlmod.plot.data_array(ds[\"northsea\"], ds, ax=axes[0][1])\n",
"nlmod.plot.data_array(ds[\"kh\"][1], ds, ax=axes[1][0])\n",
"nlmod.plot.data_array(ds[\"recharge\"][0], ds, ax=axes[1][1])"
"nlmod.plot.data_array(ds[\"recharge\"][0], ds, ax=axes[1][1]);"
]
},
{
Expand Down
25 changes: 5 additions & 20 deletions docs/examples/06_gridding_vector_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,11 @@
"sim = nlmod.sim.sim(ds)\n",
"gwf = nlmod.gwf.gwf(ds, sim)\n",
"dis = nlmod.gwf.dis(ds, gwf)\n",
"da1 = nlmod.grid.gdf_to_da(\n",
" point_gdf, ds, column=\"values\", agg_method=\"nearest\"\n",
")\n",
"da1 = nlmod.grid.gdf_to_da(point_gdf, ds, column=\"values\", agg_method=\"nearest\")\n",
"da2 = xr.DataArray(np.nan, dims=(\"y\", \"x\"), coords={\"y\": ds.y, \"x\": ds.x})\n",
"da2.values = nlmod.grid.interpolate_gdf_to_array(point_gdf, gwf, field='values', \n",
"method='linear')\n",
"da2.values = nlmod.grid.interpolate_gdf_to_array(\n",
" point_gdf, gwf, field=\"values\", method=\"linear\"\n",
")\n",
"\n",
"vmin = min(da1.min(), da2.min())\n",
"vmax = max(da1.max(), da2.max())\n",
Expand Down Expand Up @@ -575,22 +574,8 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "nlmod",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"name": "python"
}
},
"nbformat": 4,
Expand Down
8 changes: 6 additions & 2 deletions docs/examples/07_resampling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rectangular Bivariate Spline (not yet included in nlmod)"
"### Rectangular Bivariate Spline\n",
"\n",
"*Note: not yet included as a method in nlmod*"
]
},
{
Expand All @@ -251,7 +253,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rectangular Bivariate Spline with nans (not yet included in nlmod)"
"### Rectangular Bivariate Spline with nans\n",
"\n",
"*Note: not yet included as a method in nlmod*"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/08_gis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"source": [
"## Export griddata\n",
"\n",
"The model data can be exported to a netcdf file that can be visualised in Qgis. For a structured model the standard model dataset (xarray.Dataset) can be exported to a netdf file. For a vertex model you have to convert the model dataset to a certain format before you can write it to a netcdf and read it with Qgis. With the code below we export the vertex model dataset to a netcdf file ('model_qgis.nc') that can be read using Qgis. For more background, see this discussion https://github.com/ArtesiaWater/nlmod/issues/14. "
"The model data can be exported to a netcdf file that can be visualised in Qgis. For a structured model the standard model dataset (xarray.Dataset) can be exported to a netdf file. For a vertex model you have to convert the model dataset to a certain format before you can write it to a netcdf and read it with Qgis. With the code below we export the vertex model dataset to a netcdf file ('model_qgis.nc') that can be read using Qgis."
]
},
{
Expand Down
Loading

0 comments on commit 8ecca8a

Please sign in to comment.