.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples\visualize\plot_map.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_visualize_plot_map.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_visualize_plot_map.py:


Plot maps
=============

The ``imod.visualize.plot_map`` functionality of iMOD Python allows to create
customized plots.

.. GENERATED FROM PYTHON SOURCE LINES 11-12

Import the necessary packages:

.. GENERATED FROM PYTHON SOURCE LINES 12-18

.. code-block:: Python


    import numpy as np

    import imod









.. GENERATED FROM PYTHON SOURCE LINES 20-21

Import the input data to plot:

.. GENERATED FROM PYTHON SOURCE LINES 21-27

.. code-block:: Python


    tempdir = imod.util.temporary_directory()

    lakes = imod.data.lakes_shp(tempdir)
    surface_level = imod.data.ahn()["ahn"]








.. GENERATED FROM PYTHON SOURCE LINES 28-30

It is necessary to define the Matplotlib colorbar to be used and the levels
for the legend as a list.

.. GENERATED FROM PYTHON SOURCE LINES 30-33

.. code-block:: Python

    colors = "RdYlBu_r"
    levels = np.arange(-15, 17.5, 2.5)








.. GENERATED FROM PYTHON SOURCE LINES 34-35

The next lines show the simplest way to plot the raster.

.. GENERATED FROM PYTHON SOURCE LINES 35-37

.. code-block:: Python

    imod.visualize.plot_map(surface_level, colors, levels)




.. image-sg:: /examples/visualize/images/sphx_glr_plot_map_001.png
   :alt: plot map
   :srcset: /examples/visualize/images/sphx_glr_plot_map_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (<Figure size 640x480 with 2 Axes>, <Axes: >)



.. GENERATED FROM PYTHON SOURCE LINES 38-39

It is also possible to add an overlay to the previous map

.. GENERATED FROM PYTHON SOURCE LINES 39-44

.. code-block:: Python


    overlays = [{"gdf": lakes, "facecolor": "black", "alpha": 0.3}]

    imod.visualize.plot_map(surface_level, colors, levels, overlays=overlays)




.. image-sg:: /examples/visualize/images/sphx_glr_plot_map_002.png
   :alt: plot map
   :srcset: /examples/visualize/images/sphx_glr_plot_map_002.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (<Figure size 640x480 with 2 Axes>, <Axes: >)



.. GENERATED FROM PYTHON SOURCE LINES 45-46

Label the colorbar as follows:

.. GENERATED FROM PYTHON SOURCE LINES 46-50

.. code-block:: Python

    imod.visualize.plot_map(
        surface_level, colors, levels, kwargs_colorbar={"label": "Surface level (m)"}
    )




.. image-sg:: /examples/visualize/images/sphx_glr_plot_map_003.png
   :alt: plot map
   :srcset: /examples/visualize/images/sphx_glr_plot_map_003.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (<Figure size 640x480 with 2 Axes>, <Axes: >)



.. GENERATED FROM PYTHON SOURCE LINES 51-52

And to include a basemap:

.. GENERATED FROM PYTHON SOURCE LINES 52-64

.. code-block:: Python

    import contextily as ctx

    src = ctx.providers.OpenStreetMap.Mapnik
    imod.visualize.plot_map(
        surface_level,
        colors,
        levels,
        basemap=src,
        kwargs_basemap={"alpha": 0.6},
        overlays=overlays,
        kwargs_colorbar={"label": "Surface level (m)"},
    )



.. image-sg:: /examples/visualize/images/sphx_glr_plot_map_004.png
   :alt: plot map
   :srcset: /examples/visualize/images/sphx_glr_plot_map_004.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (<Figure size 640x480 with 2 Axes>, <Axes: >)




.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_examples_visualize_plot_map.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_map.ipynb <plot_map.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_map.py <plot_map.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_map.zip <plot_map.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_