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

Skip to content

Commit 9955a7c

Browse files
authored
Merge pull request #28055 from timhoffm/example-invert-axes
DOC: Improve inverted axis example
2 parents 27f6031 + 27f8f31 commit 9955a7c

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed
Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
"""
2-
===========
3-
Invert Axes
4-
===========
2+
=============
3+
Inverted axis
4+
=============
55
6-
You can use decreasing axes by flipping the normal order of the axis
7-
limits
6+
This example demonstrates two ways to invert the direction of an axis:
7+
8+
- If you want to set *explicit axis limits* anyway, e.g. via `~.Axes.set_xlim`, you
9+
can swap the limit values: ``set_xlim(4, 0)`` instead of ``set_xlim(0, 4)``.
10+
- Use `.Axis.set_inverted` if you only want to invert the axis *without modifying
11+
the limits*, i.e. keep existing limits or existing autoscaling behavior.
812
"""
913

1014
import matplotlib.pyplot as plt
1115
import numpy as np
1216

13-
t = np.arange(0.01, 5.0, 0.01)
14-
s = np.exp(-t)
17+
x = np.arange(0.01, 4.0, 0.01)
18+
y = np.exp(-x)
19+
20+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6.4, 4), layout="constrained")
21+
fig.suptitle('Inverted axis with ...')
1522

16-
fig, ax = plt.subplots()
23+
ax1.plot(x, y)
24+
ax1.set_xlim(4, 0) # inverted fixed limits
25+
ax1.set_title('fixed limits: set_xlim(4, 0)')
26+
ax1.set_xlabel('decreasing x ⟶')
27+
ax1.grid(True)
1728

18-
ax.plot(t, s)
19-
ax.set_xlim(5, 0) # decreasing time
20-
ax.set_xlabel('decreasing time (s)')
21-
ax.set_ylabel('voltage (mV)')
22-
ax.set_title('Should be growing...')
23-
ax.grid(True)
29+
ax2.plot(x, y)
30+
ax2.xaxis.set_inverted(True) # inverted axis with autoscaling
31+
ax2.set_title('autoscaling: set_inverted(True)')
32+
ax2.set_xlabel('decreasing x ⟶')
33+
ax2.grid(True)
2434

2535
plt.show()

0 commit comments

Comments
 (0)