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

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

.. _sphx_glr_gallery_images_contours_and_fields_pcolor_demo.py:


=============
pcolor images
=============

`~.Axes.pcolor` generates 2D image-style plots, as illustrated below.

.. GENERATED FROM PYTHON SOURCE LINES 8-17

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.colors import LogNorm

    # Fixing random state for reproducibility
    np.random.seed(19680801)








.. GENERATED FROM PYTHON SOURCE LINES 18-20

A simple pcolor demo
--------------------

.. GENERATED FROM PYTHON SOURCE LINES 20-34

.. code-block:: Python


    Z = np.random.rand(6, 10)

    fig, (ax0, ax1) = plt.subplots(2, 1)

    c = ax0.pcolor(Z)
    ax0.set_title('default: no edges')

    c = ax1.pcolor(Z, edgecolors='k', linewidths=4)
    ax1.set_title('thick edges')

    fig.tight_layout()
    plt.show()




.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_001.png
   :alt: default: no edges, thick edges
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_001.png, /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 35-43

Comparing pcolor with similar functions
---------------------------------------

Demonstrates similarities between `~.axes.Axes.pcolor`,
`~.axes.Axes.pcolormesh`, `~.axes.Axes.imshow` and
`~.axes.Axes.pcolorfast` for drawing quadrilateral grids.
Note that we call ``imshow`` with ``aspect="auto"`` so that it doesn't force
the data pixels to be square (the default is ``aspect="equal"``).

.. GENERATED FROM PYTHON SOURCE LINES 43-83

.. code-block:: Python


    # make these smaller to increase the resolution
    dx, dy = 0.15, 0.05

    # generate 2 2d grids for the x & y bounds
    y, x = np.mgrid[-3:3+dy:dy, -3:3+dx:dx]
    z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
    # x and y are bounds, so z should be the value *inside* those bounds.
    # Therefore, remove the last value from the z array.
    z = z[:-1, :-1]
    z_min, z_max = -abs(z).max(), abs(z).max()

    fig, axs = plt.subplots(2, 2)

    ax = axs[0, 0]
    c = ax.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
    ax.set_title('pcolor')
    fig.colorbar(c, ax=ax)

    ax = axs[0, 1]
    c = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
    ax.set_title('pcolormesh')
    fig.colorbar(c, ax=ax)

    ax = axs[1, 0]
    c = ax.imshow(z, cmap='RdBu', vmin=z_min, vmax=z_max,
                  extent=[x.min(), x.max(), y.min(), y.max()],
                  interpolation='nearest', origin='lower', aspect='auto')
    ax.set_title('image (nearest, aspect="auto")')
    fig.colorbar(c, ax=ax)

    ax = axs[1, 1]
    c = ax.pcolorfast(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
    ax.set_title('pcolorfast')
    fig.colorbar(c, ax=ax)

    fig.tight_layout()
    plt.show()





.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_002.png
   :alt: pcolor, pcolormesh, image (nearest, aspect="auto"), pcolorfast
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_002.png, /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 84-88

Pcolor with a log scale
-----------------------

The following shows pcolor plots with a log scale.

.. GENERATED FROM PYTHON SOURCE LINES 88-110

.. code-block:: Python


    N = 100
    X, Y = np.meshgrid(np.linspace(-3, 3, N), np.linspace(-2, 2, N))

    # A low hump with a spike coming out.
    # Needs to have z/colour axis on a log scale, so we see both hump and spike.
    # A linear scale only shows the spike.
    Z1 = np.exp(-X**2 - Y**2)
    Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
    Z = Z1 + 50 * Z2

    fig, (ax0, ax1) = plt.subplots(2, 1)

    c = ax0.pcolor(X, Y, Z, shading='auto',
                   norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap='PuBu_r')
    fig.colorbar(c, ax=ax0)

    c = ax1.pcolor(X, Y, Z, cmap='PuBu_r', shading='auto')
    fig.colorbar(c, ax=ax1)

    plt.show()




.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_003.png
   :alt: pcolor demo
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_003.png, /gallery/images_contours_and_fields/images/sphx_glr_pcolor_demo_003_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 111-122

.. admonition:: References

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

   - `matplotlib.axes.Axes.pcolor` / `matplotlib.pyplot.pcolor`
   - `matplotlib.axes.Axes.pcolormesh` / `matplotlib.pyplot.pcolormesh`
   - `matplotlib.axes.Axes.pcolorfast`
   - `matplotlib.axes.Axes.imshow` / `matplotlib.pyplot.imshow`
   - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`
   - `matplotlib.colors.LogNorm`


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

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


.. _sphx_glr_download_gallery_images_contours_and_fields_pcolor_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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