

.. _sphx_glr_gallery_images_contours_and_fields_contourf_hatching.py:


=================
Contourf Hatching
=================





.. rst-class:: sphx-glr-horizontal


    *

      .. image:: /gallery/images_contours_and_fields/images/sphx_glr_contourf_hatching_001.png
            :scale: 47

    *

      .. image:: /gallery/images_contours_and_fields/images/sphx_glr_contourf_hatching_002.png
            :scale: 47





.. 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()


    # ---------------------------------------------
    # |                 Plot #1                   |
    # ---------------------------------------------
    # the simplest hatched plot with a colorbar
    fig = plt.figure()
    cs = plt.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
                      cmap=plt.get_cmap('gray'),
                      extend='both', alpha=0.5
                      )
    plt.colorbar()


    # ---------------------------------------------
    # |                 Plot #2                   |
    # ---------------------------------------------
    # a plot of hatches without color with a legend
    plt.figure()
    n_levels = 6
    plt.contour(x, y, z, n_levels, colors='black', linestyles='-')
    cs = plt.contourf(x, y, z, n_levels, colors='none',
                      hatches=['.', '/', '\\', None, '\\\\', '*'],
                      extend='lower'
                      )

    # create a legend for the contour set
    artists, labels = cs.legend_elements()
    plt.legend(artists, labels, handleheight=2)


    plt.show()

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



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
