8000 DOC: Simplify axhspan example · matplotlib/matplotlib@0fc90f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fc90f6

Browse files
timhoffmstory645
andcommitted
DOC: Simplify axhspan example
- Replace the clutter of many random plot elements by one meaningful example. - Only cover spans. Remove the lines and instead cross-link to them. Co-authored-by: hannah <story645@gmail.com>
1 parent 199c31f commit 0fc90f6

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

galleries/examples/pyplots/axline.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@
5151
# - `matplotlib.axes.Axes.axhline` / `matplotlib.pyplot.axhline`
5252
# - `matplotlib.axes.Axes.axvline` / `matplotlib.pyplot.axvline`
5353
# - `matplotlib.axes.Axes.axline` / `matplotlib.pyplot.axline`
54+
#
55+
#
56+
# .. seealso::
57+
#
58+
# `~.Axes.axhspan`, `~.Axes.axvspan` draw rectangles that span the Axes in one
59+
# direction and are bounded in the other direction.
Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
11
"""
2-
============
3-
axhspan Demo
4-
============
2+
=================================
3+
Drawing regions that span an Axes
4+
=================================
55
6-
Create lines or rectangles that span the Axes in either the horizontal or
7-
vertical direction, and lines than span the Axes with an arbitrary orientation.
6+
`~.Axes.axhspan` and `~.Axes.axvspan` draw rectangles that span the Axes in either
7+
the horizontal or vertical direction and are bounded in the other direction. They are
8+
often used to highlight data regions.
89
"""
910

1011
import matplotlib.pyplot as plt
1112
import numpy as np
1213

13-
t = np.arange(-1, 2, .01)
14-
s = np.sin(2 * np.pi * t)
15-
16-
fig, ax = plt.subplots()
17-
18-
ax.plot(t, s)
19-
# Thick red horizontal line at y=0 that spans the xrange.
20-
ax.axhline(linewidth=8, color='#d62728')
21-
# Horizontal line at y=1 that spans the xrange.
22-
ax.axhline(y=1)
23-
# Vertical line at x=1 that spans the yrange.
24-
ax.axvline(x=1)
25-
# Thick blue vertical line at x=0 that spans the upper quadrant of the yrange.
26-
ax.axvline(x=0, ymin=0.75, linewidth=8, color='#1f77b4')
27-
# Default hline at y=.5 that spans the middle half of the Axes.
28-
ax.axhline(y=.5, xmin=0.25, xmax=0.75)
29-
# Infinite black line going through (0, 0) to (1, 1).
30-
ax.axline((0, 0), (1, 1), color='k')
31-
# 50%-gray rectangle spanning the Axes' width from y=0.25 to y=0.75.
32-
ax.axhspan(0.25, 0.75, facecolor='0.5')
33-
# Green rectangle spanning the Axes' height from x=1.25 to x=1.55.
34-
ax.axvspan(1.25, 1.55, facecolor='#2ca02c')
14+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 3))
15+
16+
np.random.seed(19680801)
17+
s = 2.9 * np.convolve(np.random.randn(500), np.ones(30) / 30, mode='valid')
18+
ax1.plot(s)
19+
ax1.axhspan(-1, 1, alpha=0.1)
20+
ax1.set_ylim(-1.5, 1.5)
21+
22+
23+
mu = 8
24+
sigma = 2
25+
x = np.linspace(0, 16, 401)
26+
y = np.exp(-((x-mu)**2)/(2*sigma**2))
27+
ax2.axvspan(mu-2*sigma, mu-sigma, color='0.95')
28+
ax2.axvspan(mu-sigma, mu+sigma, color='0.9')
29+
ax2.axvspan(mu+sigma, mu+2*sigma, color='0.95')
30+
ax2.axvline(mu, color='darkgrey', linestyle='--')
31+
ax2.plot(x, y)
3532

3633
plt.show()
34+
35+
# %%
36+
#
37+
# .. admonition:: References
38+
#
39+
# The use of the following functions, methods, classes and modules is shown
40+
# in this example:
41+
#
42+
# - `matplotlib.axes.Axes.axhspan` / `matplotlib.pyplot.axhspan`
43+
# - `matplotlib.axes.Axes.axvspan` / `matplotlib.pyplot.axvspan`
44+
#
45+
#
46+
# .. seealso::
47+
#
48+
# `~.Axes.axhline`, `~.Axes.axvline`, `~.Axes.axline` draw infinite lines.

0 commit comments

Comments
 (0)
0