
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/images_contours_and_fields/contourf_hatching.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

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

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

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

.. _sphx_glr_gallery_images_contours_and_fields_contourf_hatching.py:


=================
Contourf hatching
=================

Demo filled contour plots with hatched patterns.

.. GENERATED FROM PYTHON SOURCE LINES 8-20

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    # invent some numbers, turning the x and y arrays into simple
    # 2d arrays, which make combining them together easier.
    x = np.linspace(-3, 5, 150).reshape(1, -1)
    y = np.linspace(-3, 5, 120).reshape(-1, 1)
    z = np.cos(x) + np.sin(y)

    # we no longer need x and y to be 2 dimensional, so flatten them.
    x, y = x.flatten(), y.flatten()








.. GENERATED FROM PYTHON SOURCE LINES 21-22

Plot 1: the simplest hatched plot with a colorbar

.. GENERATED FROM PYTHON SOURCE LINES 22-28

.. code-block:: Python


    fig1, ax1 = plt.subplots()
    cs = ax1.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
                      cmap='gray', extend='both', alpha=0.5)
    fig1.colorbar(cs)




.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_contourf_hatching_001.png
   :alt: contourf hatching
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_contourf_hatching_001.png, /gallery/images_contours_and_fields/images/sphx_glr_contourf_hatching_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 29-30

Plot 2: a plot of hatches without color with a legend

.. GENERATED FROM PYTHON SOURCE LINES 30-43

.. code-block:: Python


    fig2, ax2 = plt.subplots()
    n_levels = 6
    ax2.contour(x, y, z, n_levels, colors='black', linestyles='-')
    cs = ax2.contourf(x, y, z, n_levels, colors='none',
                      hatches=['.', '/', '\\', None, '\\\\', '*'],
                      extend='lower')

    # create a legend for the contour set
    artists, labels = cs.legend_elements(str_format='{:2.1f}'.format)
    ax2.legend(artists, labels, handleheight=2, framealpha=1)
    plt.show()




.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_contourf_hatching_002.png
   :alt: contourf hatching
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_contourf_hatching_002.png, /gallery/images_contours_and_fields/images/sphx_glr_contourf_hatching_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 44-55

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.axes.Axes.contour` / `matplotlib.pyplot.contour`
   - `matplotlib.axes.Axes.contourf` / `matplotlib.pyplot.contourf`
   - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`
   - `matplotlib.axes.Axes.legend` / `matplotlib.pyplot.legend`
   - `matplotlib.contour.ContourSet`
   - `matplotlib.contour.ContourSet.legend_elements`


.. _sphx_glr_download_gallery_images_contours_and_fields_contourf_hatching.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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