
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/contour.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_contour.py>`
        to download the full example code

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

.. _sphx_glr_examples_contour.py:


Contour plots
=============

Contour plot support is limited to picking the individual
:class:`~matplotlib.collections.LineCollection`\s, which are directly
registered with the axes and thus picked up by `mplcursors.cursor`
(:class:`~matplotlib.contour.QuadContourSet`\s are not even artists, which make
them hard to handle without additional special-casing).  It remains possible to
retrieve the ``z`` value and add it manually to the annotation, though.

.. GENERATED FROM PYTHON SOURCE LINES 12-33



.. image-sg:: /examples/images/sphx_glr_contour_001.png
   :alt: contour
   :srcset: /examples/images/sphx_glr_contour_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import numpy as np
    import matplotlib.pyplot as plt
    import mplcursors

    np.random.seed(42)

    fig, ax = plt.subplots()
    cf = ax.contour(np.random.random((10, 10)))
    cursor = mplcursors.cursor()

    @cursor.connect("add")
    def on_add(sel):
        ann = sel.annotation
        # `cf.collections.index(sel.artist)` is the index of the selected line
        # among all those that form the contour plot.
        # `cf.cvalues[...]` is the corresponding value.
        ann.set_text("{}\nz={:.3g}".format(
            ann.get_text(), cf.cvalues[cf.collections.index(sel.artist)]))

    plt.show()


.. _sphx_glr_download_examples_contour.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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