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

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

.. _sphx_glr_gallery_images_contours_and_fields_image_nonuniform.py:


================
Image nonuniform
================

`.NonUniformImage` is a generalized image with pixels on a rectilinear grid,
i.e. it allows rows and columns with individual heights / widths.

There is no high-level plotting method on `~.axes.Axes` or `.pyplot` to
create a NonUniformImage. Instead, you have to instantiate the image
explicitly add it to the Axes using `.Axes.add_image`.

.. GENERATED FROM PYTHON SOURCE LINES 13-73



.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_image_nonuniform_001.png
   :alt: NonUniformImage class, nearest, nearest, bilinear, bilinear
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_image_nonuniform_001.png, /gallery/images_contours_and_fields/images/sphx_glr_image_nonuniform_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib import cm
    from matplotlib.image import NonUniformImage

    interp = 'nearest'

    # Linear x array for cell centers:
    x = np.linspace(-4, 4, 9)

    # Highly nonlinear x array:
    x2 = x**3

    y = np.linspace(-4, 4, 9)

    z = np.sqrt(x[np.newaxis, :]**2 + y[:, np.newaxis]**2)

    fig, axs = plt.subplots(nrows=2, ncols=2, layout='constrained')
    fig.suptitle('NonUniformImage class', fontsize='large')
    ax = axs[0, 0]
    im = NonUniformImage(ax, interpolation=interp, extent=(-4, 4, -4, 4),
                         cmap=cm.Purples)
    im.set_data(x, y, z)
    ax.add_image(im)
    ax.set_xlim(-4, 4)
    ax.set_ylim(-4, 4)
    ax.set_title(interp)

    ax = axs[0, 1]
    im = NonUniformImage(ax, interpolation=interp, extent=(-64, 64, -4, 4),
                         cmap=cm.Purples)
    im.set_data(x2, y, z)
    ax.add_image(im)
    ax.set_xlim(-64, 64)
    ax.set_ylim(-4, 4)
    ax.set_title(interp)

    interp = 'bilinear'

    ax = axs[1, 0]
    im = NonUniformImage(ax, interpolation=interp, extent=(-4, 4, -4, 4),
                         cmap=cm.Purples)
    im.set_data(x, y, z)
    ax.add_image(im)
    ax.set_xlim(-4, 4)
    ax.set_ylim(-4, 4)
    ax.set_title(interp)

    ax = axs[1, 1]
    im = NonUniformImage(ax, interpolation=interp, extent=(-64, 64, -4, 4),
                         cmap=cm.Purples)
    im.set_data(x2, y, z)
    ax.add_image(im)
    ax.set_xlim(-64, 64)
    ax.set_ylim(-4, 4)
    ax.set_title(interp)

    plt.show()


.. _sphx_glr_download_gallery_images_contours_and_fields_image_nonuniform.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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