|
1 | 1 | """
|
2 |
| -=========== |
3 |
| -Invert Axes |
4 |
| -=========== |
| 2 | +============= |
| 3 | +Inverted axis |
| 4 | +============= |
5 | 5 |
|
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. |
8 | 12 | """
|
9 | 13 |
|
10 | 14 | import matplotlib.pyplot as plt
|
11 | 15 | import numpy as np
|
12 | 16 |
|
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 ...') |
15 | 22 |
|
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) |
17 | 28 |
|
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) |
24 | 34 |
|
25 | 35 | plt.show()
|
0 commit comments