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

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

.. _sphx_glr_gallery_misc_zorder_demo.py:


===========
Zorder Demo
===========

The drawing order of artists is determined by their ``zorder`` attribute, which
is a floating point number. Artists with higher ``zorder`` are drawn on top.
You can change the order for individual artists by setting their ``zorder``.
The default value depends on the type of the Artist:

================================================================    =======
Artist                                                              Z-order
================================================================    =======
Images (`.AxesImage`, `.FigureImage`, `.BboxImage`)                 0
`.Patch`, `.PatchCollection`                                        1
`.Line2D`, `.LineCollection` (including minor ticks, grid lines)    2
Major ticks                                                         2.01
`.Text` (including Axes labels and titles)                          3
`.Legend`                                                           5
================================================================    =======

Any call to a plotting method can set a value for the zorder of that particular
item explicitly.

.. note::

   `~.axes.Axes.set_axisbelow` and :rc:`axes.axisbelow` are convenient helpers
   for setting the zorder of ticks and grid lines.

Drawing is done per `~.axes.Axes` at a time. If you have overlapping Axes, all
elements of the second Axes are drawn on top of the first Axes, irrespective of
their relative zorder.

.. GENERATED FROM PYTHON SOURCE LINES 34-43

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    r = np.linspace(0.3, 1, 30)
    theta = np.linspace(0, 4*np.pi, 30)
    x = r * np.sin(theta)
    y = r * np.cos(theta)








.. GENERATED FROM PYTHON SOURCE LINES 44-49

The following example contains a `.Line2D` created by `~.axes.Axes.plot()`
and the dots (a `.PatchCollection`) created by `~.axes.Axes.scatter()`.
Hence, by default the dots are below the line (first subplot).
In the second subplot, the ``zorder`` is set explicitly to move the dots
on top of the line.

.. GENERATED FROM PYTHON SOURCE LINES 49-62

.. code-block:: Python


    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3.2))

    ax1.plot(x, y, 'C3', lw=3)
    ax1.scatter(x, y, s=120)
    ax1.set_title('Lines on top of dots')

    ax2.plot(x, y, 'C3', lw=3)
    ax2.scatter(x, y, s=120, zorder=2.5)  # move dots on top of line
    ax2.set_title('Dots on top of lines')

    plt.tight_layout()




.. image-sg:: /gallery/misc/images/sphx_glr_zorder_demo_001.png
   :alt: Lines on top of dots, Dots on top of lines
   :srcset: /gallery/misc/images/sphx_glr_zorder_demo_001.png, /gallery/misc/images/sphx_glr_zorder_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 63-65

Many functions that create a visible object accepts a ``zorder`` parameter.
Alternatively, you can call ``set_zorder()`` on the created object later.

.. GENERATED FROM PYTHON SOURCE LINES 65-76

.. code-block:: Python


    x = np.linspace(0, 7.5, 100)
    plt.rcParams['lines.linewidth'] = 5
    plt.figure()
    plt.plot(x, np.sin(x), label='zorder=2', zorder=2)  # bottom
    plt.plot(x, np.sin(x+0.5), label='zorder=3',  zorder=3)
    plt.axhline(0, label='zorder=2.5', color='lightgrey', zorder=2.5)
    plt.title('Custom order of elements')
    l = plt.legend(loc='upper right')
    l.set_zorder(2.5)  # legend between blue and orange line
    plt.show()



.. image-sg:: /gallery/misc/images/sphx_glr_zorder_demo_002.png
   :alt: Custom order of elements
   :srcset: /gallery/misc/images/sphx_glr_zorder_demo_002.png, /gallery/misc/images/sphx_glr_zorder_demo_002_2_00x.png 2.00x
   :class: sphx-glr-single-img






.. _sphx_glr_download_gallery_misc_zorder_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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