8000 DOC: Simpify markevery demo · matplotlib/matplotlib@c398489 · GitHub
[go: up one dir, main page]

Skip to content

Commit c398489

Browse files
committed
DOC: Simpify markevery demo
- Reword description - Remove non-distinguishing examples: We don't need three float examples, (0.0, 0.1) is the same as 0.1. - This makes for 9 cases, which can be laid out easily in a 3x3 grid, so we don't need axes calculation and removal logic.
1 parent be94172 commit c398489

File tree

1 file changed

+65
-70
lines changed

1 file changed

+65
-70
lines changed

examples/lines_bars_and_markers/markevery_demo.py

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,99 +3,94 @@
33
Markevery Demo
44
==============
55
6-
This example demonstrates the various options for showing a marker at a
7-
subset of data points using the ``markevery`` property of a Line2D object.
8-
9-
Integer arguments are fairly intuitive. e.g. ``markevery=5`` will plot every
10-
5th marker starting from the first data point.
11-
12-
Float arguments allow markers to be spaced at approximately equal distances
13-
along the line. The theoretical distance along the line between markers is
14-
determined by multiplying the display-coordinate distance of the axes
15-
bounding-box diagonal by the value of ``markevery``. The data points closest
16-
to the theoretical distances will be shown.
17-
18-
A slice or list/array can also be used with ``markevery`` to specify the
19-
markers to show.
6+
The ``markevery`` property of `.Line2D` allows drawing markers only at a
7+
subset of data points.
8+
9+
The exact list of possible parameters is specified at `.Line2D.set_markevery`.
10+
In short:
11+
12+
- A single integer N draws every N-th marker.
13+
- A tuple of integers (start, N) draws every N-th marker, starting at index
14+
*start*.
15+
- A list of integers draws the markers at the specified indices.
16+
- A slice draws the markers at the sliced indices.
17+
- A float results in approximately even spacing along the curve. The distance
18+
between markers is length of the Axes diagonal in data coordinates
19+
multiplied by the given float.
2020
"""
2121

2222
import numpy as np
2323
import matplotlib.pyplot as plt
2424

2525
# define a list of markevery cases to plot
26-
cases = [None,
27-
8,
28-
(30, 8),
29-
[16, 24, 30], [0, -1],
30-
slice(100, 200, 3),
31-
0.1, 0.3, 1.5,
32-
(0.0, 0.1), (0.45, 0.1)]
33-
34-
# define the figure size and grid layout properties
35-
figsize = (10, 8)
36-
cols = 3
37-
rows = len(cases) // cols + 1
< 10000 /code>
38-
# define the data for cartesian plots
26+
cases = [
27+
None,
28+
8,
29+
(30, 8),
30+
[16, 24, 32],
31+
[0, -1],
32+
slice(100, 200, 3),
33+
0.1,
34+
0.4,
35+
(0.2, 0.4)
36+
]
37+
38+
# data points
3939
delta = 0.11
4040
x = np.linspace(0, 10 - 2 * delta, 200) + delta
4141
y = np.sin(x) + 1.0 + delta
4242

43-
44-
def trim_axs(axs, N):
45-
"""
46-
Reduce *axs* to *N* Axes. All further Axes are removed from the figure.
47-
"""
48-
axs = axs.flat
49-
for ax in axs[N:]:
50-
ax.remove()
51-
return axs[:N]
52-
5343
###############################################################################
54-
# Plot each markevery case for linear x and y scales
44+
# markevery with linear sacles
45+
# ----------------------------
5546

56-
axs = plt.figure(figsize=figsize, constrained_layout=True).subplots(rows, cols)
57-
axs = trim_axs(axs, len(cases))
58-
for ax, case in zip(axs, cases):
59-
ax.set_title('markevery=%s' % str(case))
60-
ax.plot(x, y, 'o', ls='-', ms=4, markevery=case)
47+
fig, axs = plt.subplots(3, 3, figsize=(10, 6), constrained_layout=True)
48+
for ax, markevery in zip(axs.flat, cases):
49+
ax.set_title(f'markevery={markevery}')
50+
ax.plot(x, y, 'o', ls='-', ms=4, markevery=markevery)
6151

6252
###############################################################################
63-
# Plot each markevery case for log x and y scales
64-
65-
axs = plt.figure(figsize=figsize, constrained_layout=True).subplots(rows, cols)
66-
axs = trim_axs(axs, len(cases))
67-
for ax, case in zip(axs, cases):
68-
ax.set_title('markevery=%s' % str(case))
53+
# markevery with log scales
54+
# -------------------------
55+
#
56+
# Note that the log scale causes a visual asymmetry in the marker distance for
57+
# the integer-based subsampling. In contrast, the float-based subsampling
58+
# creates even distributions, because it's based on fractions of the Axes
59+
# diagonal, not on data coordinates or data indices.
60+
61+
fig, axs = plt.subplots(3, 3, figsize=(10, 6), constrained_layout=True)
62+
for ax, markevery in zip(axs.flat, cases):
63+
ax.set_title(f'markevery={markevery}')
6964
ax.set_xscale('log')
7065
ax.set_yscale('log')
71-
ax.plot(x, y, 'o', ls='-', ms=4, markevery=case)
66+
ax.plot(x, y, 'o', ls='-', ms=4, markevery=markevery)
7267

7368
###############################################################################
74-
# Plot each markevery case for linear x and y scales but zoomed in
75-
# note the behaviour when zoomed in. When a start marker offset is specified
76-
# it is always interpreted with respect to the first data point which might be
77-
# different to the first visible data point.
78-
79-
axs = plt.figure(figsize=figsize, constrained_layout=True).subplots(rows, cols)
80-
axs = trim_axs(axs, len(cases))
81-
for ax, case in zip(axs, cases):
82-
ax.set_title('markevery=%s' % str(case))
83-
ax.plot(x, y, 'o', ls='-', ms=4, markevery=case)
69+
# markevery on zoomed plots
70+
# -------------------------
71+
#
72+
# When a start marker offset is specified it is always interpreted with
73+
# respect to the first data point which might be different to the first
74+
# visible data point.
75+
76+
fig, axs = plt.subplots(3, 3, figsize=(10, 6), constrained_layout=True)
77+
for ax, markevery in zip(axs.flat, cases):
78+
ax.set_title(f'markevery={markevery}')
79+
ax.plot(x, y, 'o', ls='-', ms=4, markevery=markevery)
8480
ax.set_xlim((6, 6.7))
8581
ax.set_ylim((1.1, 1.7))
8682

87-
# define data for polar plots
83+
###############################################################################
84+
# markevery on polar plots
85+
# ------------------------
86+
8887
r = np.linspace(0, 3.0, 200)
8988
theta = 2 * np.pi * r
9089

91-
###############################################################################
92-
# Plot each markevery case for polar plots
93-
94-
axs = plt.figure(figsize=figsize, constrained_layout=True).subplots(
95-
rows, cols, subplot_kw={'projection': 'polar'})
96-
axs = trim_axs(axs, len(cases))
97-
for ax, case in zip(axs, cases):
98-
ax.set_title('markevery=%s' % str(case))
99-
ax.plot(theta, r, 'o', ls='-', ms=4, markevery=case)
90+
fig, axs = plt.subplots(3, 3, figsize=(10, 6), constrained_layout=True,
91+
subplot_kw={'projection': 'polar'})
92+
for ax, markevery in zip(axs.flat, cases):
93+
ax.set_title(f'markevery={markevery}')
94+
ax.plot(theta, r, 'o', ls='-', ms=4, markevery=markevery)
10095

10196
plt.show()

0 commit comments

Comments
 (0)
0