|
3 | 3 | Zorder Demo
|
4 | 4 | ===========
|
5 | 5 |
|
6 |
| -The default drawing order for axes is patches, lines, text. This |
7 |
| -order is determined by the zorder attribute. The following defaults |
8 |
| -are set |
| 6 | +The drawing order of artists is determined by their ``zorder`` attribute, which |
| 7 | +is a floating point number. Artists with higher ``zorder`` are drawn on top. |
| 8 | +You can change the order for individual artists by setting their ``zorder``. |
| 9 | +The default value depends on the type of the Artist: |
9 | 10 |
|
10 |
| -======================= ======= |
11 |
| -Artist Z-order |
12 |
| -======================= ======= |
13 |
| -Patch / PatchCollection 1 |
14 |
| -Line2D / LineCollection 2 |
15 |
| -Text 3 |
16 |
| -======================= ======= |
| 11 | +================================================================ ======= |
| 12 | +Artist Z-order |
| 13 | +================================================================ ======= |
| 14 | +Images (`.AxesImage`, `.FigureImage`, `.BboxImage`) 0 |
| 15 | +`.Patch`, `.PatchCollection` 1 |
| 16 | +`.Line2D`, `.LineCollection` (including minor ticks, grid lines) 2 |
| 17 | +Major ticks 2.01 |
| 18 | +`.Text` (including axes labels and titles) 3 |
| 19 | +`.Legend` 5 |
| 20 | +================================================================ ======= |
17 | 21 |
|
18 |
| -You can change the order for individual artists by setting the zorder. Any |
19 |
| -individual plot() call can set a value for the zorder of that particular item. |
| 22 | +Any call to a plotting method can set a value for the zorder of that particular |
| 23 | +item explicitly. |
20 | 24 |
|
21 |
| -In the fist subplot below, the lines are drawn above the patch |
22 |
| -collection from the scatter, which is the default. |
| 25 | +.. note:: |
23 | 26 |
|
24 |
| -In the subplot below, the order is reversed. |
25 |
| -
|
26 |
| -The second figure shows how to control the zorder of individual lines. |
| 27 | + `~.axes.Axes.set_axisbelow` and :rc:`axes.axisbelow` can further modify the |
| 28 | + zorder of ticks and grid lines. |
27 | 29 | """
|
28 | 30 |
|
29 | 31 | import matplotlib.pyplot as plt
|
30 | 32 | import numpy as np
|
31 | 33 |
|
32 |
| -# Fixing random state for reproducibility |
33 |
| -np.random.seed(19680801) |
| 34 | +r = np.linspace(0.3, 1, 30) |
| 35 | +theta = np.linspace(0, 4*np.pi, 30) |
| 36 | +x = r * np.sin(theta) |
| 37 | +y = r * np.cos(theta) |
34 | 38 |
|
| 39 | +############################################################################### |
| 40 | +# The following example contains a `.Line2D` created by `~.axes.Axes.plot()` |
| 41 | +# and the dots (a `.PatchCollection`) created by `~.axes.Axes.scatter()`. |
| 42 | +# Hence, by default the dots are below the line (first subplot). |
| 43 | +# In the second subplot, the ``zorder`` is set explicitly to move the dots |
| 44 | +# on top of the line. |
35 | 45 |
|
36 |
| -x = np.random.random(20) |
37 |
| -y = np.random.random(20) |
| 46 | +fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3.2)) |
38 | 47 |
|
39 |
| -############################################################################### |
40 |
| -# Lines on top of scatter |
| 48 | +ax1.plot(x, y, 'C3', lw=3) |
| 49 | +ax1.scatter(x, y, s=120) |
| 50 | +ax1.set_title('Lines on top of dots') |
41 | 51 |
|
42 |
| -plt.figure() |
43 |
| -plt.subplot(211) |
44 |
| -plt.plot(x, y, 'C3', lw=3) |
45 |
| -plt.scatter(x, y, s=120) |
46 |
| -plt.title('Lines on top of dots') |
| 52 | +ax2.plot(x, y, 'C3', lw=3) |
| 53 | +ax2.scatter(x, y, s=120, zorder=2.5) # move dots on top of line |
| 54 | +ax2.set_title('Dots on top of lines') |
47 | 55 |
|
48 |
| -# Scatter plot on top of lines |
49 |
| -plt.subplot(212) |
50 |
| -plt.plot(x, y, 'C3', zorder=1, lw=3) |
51 |
| -plt.scatter(x, y, s=120, zorder=2) |
52 |
| -plt.title('Dots on top of lines') |
53 | 56 | plt.tight_layout()
|
54 | 57 |
|
55 | 58 | ###############################################################################
|
56 |
| -# A new figure, with individually ordered items |
| 59 | +# Many functions that create a visible object accepts a ``zorder`` parameter. |
| 60 | +# Alternatively, you can call ``set_order()`` on the created object later. |
57 | 61 |
|
58 |
| -x = np.linspace(0, 2*np.pi, 100) |
59 |
| -plt.rcParams['lines.linewidth'] = 10 |
| 62 | +x = np.linspace(0, 7.5, 100) |
| 63 | +plt.rcParams['lines.linewidth'] = 5 |
60 | 64 | plt.figure()
|
61 |
| -plt.plot(x, np.sin(x), label='zorder=10', zorder=10) # on top |
62 |
| -plt.plot(x, np.sin(1.1*x), label='zorder=1', zorder=1) # bottom |
63 |
| -plt.plot(x, np.sin(1.2*x), label='zorder=3', zorder=3) |
64 |
| -plt.axhline(0, label='zorder=2', color='grey', zorder=2) |
| 65 | +plt.plot(x, np.sin(x), label='zorder=2', zorder=2) # bottom |
| 66 | +plt.plot(x, np.sin(x+0.5), label='zorder=3', zorder=3) |
| 67 | +plt.axhline(0, label='zorder=2.5', color='lightgrey', zorder=2.5) |
65 | 68 | plt.title('Custom order of elements')
|
66 | 69 | l = plt.legend(loc='upper right')
|
67 |
| -l.set_zorder(20) # put the legend on top |
| 70 | +l.set_zorder(2.5) # legend between blue and orange line |
68 | 71 | plt.show()
|
0 commit comments