
.. 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_zcoord.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_zcoord.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_images_contours_and_fields_image_zcoord.py:


==================================
Modifying the coordinate formatter
==================================

Modify the coordinate formatter to report the image "z" value of the nearest
pixel given x and y.  This functionality is built in by default; this example
just showcases how to customize the `~.axes.Axes.format_coord` function.

.. GENERATED FROM PYTHON SOURCE LINES 10-38

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

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


    X = 10*np.random.rand(5, 3)

    fig, ax = plt.subplots()
    ax.imshow(X)


    def format_coord(x, y):
        col = round(x)
        row = round(y)
        nrows, ncols = X.shape
        if 0 <= col < ncols and 0 <= row < nrows:
            z = X[row, col]
            return f'x={x:1.4f}, y={y:1.4f}, z={z:1.4f}'
        else:
            return f'x={x:1.4f}, y={y:1.4f}'


    ax.format_coord = format_coord
    plt.show()




.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_image_zcoord_001.png
   :alt: image zcoord
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_image_zcoord_001.png, /gallery/images_contours_and_fields/images/sphx_glr_image_zcoord_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 39-46

.. admonition:: References

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

   - `matplotlib.axes.Axes.format_coord`
   - `matplotlib.axes.Axes.imshow`


.. _sphx_glr_download_gallery_images_contours_and_fields_image_zcoord.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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