
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/specialty_plots/advanced_hillshading.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_specialty_plots_advanced_hillshading.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_specialty_plots_advanced_hillshading.py:


===========
Hillshading
===========

Demonstrates a few common tricks with shaded plots.

.. GENERATED FROM PYTHON SOURCE LINES 8-77



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


    *

      .. image-sg:: /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_001.png
         :alt: Using a colorbar with a shaded plot
         :srcset: /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_001.png, /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_001_2_00x.png 2.00x
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_002.png
         :alt: Avoiding Outliers in Shaded Plots, Full range of data, Manually set range
         :srcset: /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_002.png, /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_002_2_00x.png 2.00x
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_003.png
         :alt: Shade by one variable, color by another
         :srcset: /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_003.png, /gallery/specialty_plots/images/sphx_glr_advanced_hillshading_003_2_00x.png 2.00x
         :class: sphx-glr-multi-img





.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.colors import LightSource, Normalize


    def display_colorbar():
        """Display a correct numeric colorbar for a shaded plot."""
        y, x = np.mgrid[-4:2:200j, -4:2:200j]
        z = 10 * np.cos(x**2 + y**2)

        cmap = plt.cm.copper
        ls = LightSource(315, 45)
        rgb = ls.shade(z, cmap)

        fig, ax = plt.subplots()
        ax.imshow(rgb, interpolation='bilinear')

        # Use a proxy artist for the colorbar...
        im = ax.imshow(z, cmap=cmap)
        im.remove()
        fig.colorbar(im, ax=ax)

        ax.set_title('Using a colorbar with a shaded plot', size='x-large')


    def avoid_outliers():
        """Use a custom norm to control the displayed z-range of a shaded plot."""
        y, x = np.mgrid[-4:2:200j, -4:2:200j]
        z = 10 * np.cos(x**2 + y**2)

        # Add some outliers...
        z[100, 105] = 2000
        z[120, 110] = -9000

        ls = LightSource(315, 45)
        fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4.5))

        rgb = ls.shade(z, plt.cm.copper)
        ax1.imshow(rgb, interpolation='bilinear')
        ax1.set_title('Full range of data')

        rgb = ls.shade(z, plt.cm.copper, vmin=-10, vmax=10)
        ax2.imshow(rgb, interpolation='bilinear')
        ax2.set_title('Manually set range')

        fig.suptitle('Avoiding Outliers in Shaded Plots', size='x-large')


    def shade_other_data():
        """Demonstrates displaying different variables through shade and color."""
        y, x = np.mgrid[-4:2:200j, -4:2:200j]
        z1 = np.sin(x**2)  # Data to hillshade
        z2 = np.cos(x**2 + y**2)  # Data to color

        norm = Normalize(z2.min(), z2.max())
        cmap = plt.cm.RdBu

        ls = LightSource(315, 45)
        rgb = ls.shade_rgb(cmap(norm(z2)), z1)

        fig, ax = plt.subplots()
        ax.imshow(rgb, interpolation='bilinear')
        ax.set_title('Shade by one variable, color by another', size='x-large')

    display_colorbar()
    avoid_outliers()
    shade_other_data()
    plt.show()


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

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


.. _sphx_glr_download_gallery_specialty_plots_advanced_hillshading.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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