{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Zorder Demo\n\nThe drawing order of artists is determined by their ``zorder`` attribute, which\nis a floating point number. Artists with higher ``zorder`` are drawn on top.\nYou can change the order for individual artists by setting their ``zorder``.\nThe default value depends on the type of the Artist:\n\n================================================================    =======\nArtist                                                              Z-order\n================================================================    =======\nImages (`.AxesImage`, `.FigureImage`, `.BboxImage`)                 0\n`.Patch`, `.PatchCollection`                                        1\n`.Line2D`, `.LineCollection` (including minor ticks, grid lines)    2\nMajor ticks                                                         2.01\n`.Text` (including Axes labels and titles)                          3\n`.Legend`                                                           5\n================================================================    =======\n\nAny call to a plotting method can set a value for the zorder of that particular\nitem explicitly.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>`~.axes.Axes.set_axisbelow` and :rc:`axes.axisbelow` are convenient helpers\n   for setting the zorder of ticks and grid lines.</p></div>\n\nDrawing is done per `~.axes.Axes` at a time. If you have overlapping Axes, all\nelements of the second Axes are drawn on top of the first Axes, irrespective of\ntheir relative zorder.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nr = np.linspace(0.3, 1, 30)\ntheta = np.linspace(0, 4*np.pi, 30)\nx = r * np.sin(theta)\ny = r * np.cos(theta)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following example contains a `.Line2D` created by `~.axes.Axes.plot()`\nand the dots (a `.PatchCollection`) created by `~.axes.Axes.scatter()`.\nHence, by default the dots are below the line (first subplot).\nIn the second subplot, the ``zorder`` is set explicitly to move the dots\non top of the line.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3.2))\n\nax1.plot(x, y, 'C3', lw=3)\nax1.scatter(x, y, s=120)\nax1.set_title('Lines on top of dots')\n\nax2.plot(x, y, 'C3', lw=3)\nax2.scatter(x, y, s=120, zorder=2.5)  # move dots on top of line\nax2.set_title('Dots on top of lines')\n\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Many functions that create a visible object accepts a ``zorder`` parameter.\nAlternatively, you can call ``set_zorder()`` on the created object later.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.linspace(0, 7.5, 100)\nplt.rcParams['lines.linewidth'] = 5\nplt.figure()\nplt.plot(x, np.sin(x), label='zorder=2', zorder=2)  # bottom\nplt.plot(x, np.sin(x+0.5), label='zorder=3',  zorder=3)\nplt.axhline(0, label='zorder=2.5', color='lightgrey', zorder=2.5)\nplt.title('Custom order of elements')\nl = plt.legend(loc='upper right')\nl.set_zorder(2.5)  # legend between blue and orange line\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}