Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 032a193

Browse files
authored
Merge pull request #15619 from timhoffm/doc-zorder-demo
Improve zorder demo
2 parents 08008d5 + 9937d33 commit 032a193

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

examples/misc/zorder_demo.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,69 @@
33
Zorder Demo
44
===========
55
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:
910
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+
================================================================ =======
1721
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.
2024
21-
In the fist subplot below, the lines are drawn above the patch
22-
collection from the scatter, which is the default.
25+
.. note::
2326
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.
2729
"""
2830

2931
import matplotlib.pyplot as plt
3032
import numpy as np
3133

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)
3438

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.
3545

36-
x = np.random.random(20)
37-
y = np.random.random(20)
46+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3.2))
3847

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')
4151

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')
4755

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')
5356
plt.tight_layout()
5457

5558
###############################################################################
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.
5761

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
6064
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)
6568
plt.title('Custom order of elements')
6669
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
6871
plt.show()

0 commit comments

Comments
 (0)