|
1 | 1 | r""" |
2 | | -========================== |
3 | | -Multiple Yaxis With Spines |
4 | | -========================== |
| 2 | +=========================== |
| 3 | +Multiple y-axis with Spines |
| 4 | +=========================== |
5 | 5 |
|
6 | 6 | Create multiple y axes with a shared x axis. This is done by creating |
7 | 7 | a `~.axes.Axes.twinx` axes, turning all spines but the right one invisible |
8 | 8 | and offset its position using `~.spines.Spine.set_position`. |
9 | 9 |
|
10 | 10 | Note that this approach uses `matplotlib.axes.Axes` and their |
11 | | -`~matplotlib.spines.Spine`\s. An alternative approach for parasite |
12 | | -axes is shown in the :doc:`/gallery/axisartist/demo_parasite_axes` and |
| 11 | +`~matplotlib.spines.Spine`\s. Alternative approaches using non-standard axes |
| 12 | +are shown in the :doc:`/gallery/axisartist/demo_parasite_axes` and |
13 | 13 | :doc:`/gallery/axisartist/demo_parasite_axes2` examples. |
14 | 14 | """ |
15 | 15 |
|
16 | 16 | import matplotlib.pyplot as plt |
17 | 17 |
|
18 | | - |
19 | 18 | fig, ax = plt.subplots() |
20 | 19 | fig.subplots_adjust(right=0.75) |
21 | 20 |
|
|
26 | 25 | # placed on the right by twinx above. |
27 | 26 | twin2.spines.right.set_position(("axes", 1.2)) |
28 | 27 |
|
29 | | -p1, = ax.plot([0, 1, 2], [0, 1, 2], "b-", label="Density") |
30 | | -p2, = twin1.plot([0, 1, 2], [0, 3, 2], "r-", label="Temperature") |
31 | | -p3, = twin2.plot([0, 1, 2], [50, 30, 15], "g-", label="Velocity") |
32 | | - |
33 | | -ax.set_xlim(0, 2) |
34 | | -ax.set_ylim(0, 2) |
35 | | -twin1.set_ylim(0, 4) |
36 | | -twin2.set_ylim(1, 65) |
| 28 | +p1, = ax.plot([0, 1, 2], [0, 1, 2], "C0", label="Density") |
| 29 | +p2, = twin1.plot([0, 1, 2], [0, 3, 2], "C1", label="Temperature") |
| 30 | +p3, = twin2.plot([0, 1, 2], [50, 30, 15], "C2", label="Velocity") |
37 | 31 |
|
38 | | -ax.set_xlabel("Distance") |
39 | | -ax.set_ylabel("Density") |
40 | | -twin1.set_ylabel("Temperature") |
41 | | -twin2.set_ylabel("Velocity") |
| 32 | +ax.set(xlim=(0, 2), ylim=(0, 2), xlabel="Distance", ylabel="Density") |
| 33 | +twin1.set(ylim=(0, 4), ylabel="Temperature") |
| 34 | +twin2.set(ylim=(1, 65), ylabel="Velocity") |
42 | 35 |
|
43 | 36 | ax.yaxis.label.set_color(p1.get_color()) |
44 | 37 | twin1.yaxis.label.set_color(p2.get_color()) |
45 | 38 | twin2.yaxis.label.set_color(p3.get_color()) |
46 | 39 |
|
47 | | -tkw = dict(size=4, width=1.5) |
48 | | -ax.tick_params(axis='y', colors=p1.get_color(), **tkw) |
49 | | -twin1.tick_params(axis='y', colors=p2.get_color(), **tkw) |
50 | | -twin2.tick_params(axis='y', colors=p3.get_color(), **tkw) |
51 | | -ax.tick_params(axis='x', **tkw) |
| 40 | +ax.tick_params(axis='y', colors=p1.get_color()) |
| 41 | +twin1.tick_params(axis='y', colors=p2.get_color()) |
| 42 | +twin2.tick_params(axis='y', colors=p3.get_color()) |
52 | 43 |
|
53 | 44 | ax.legend(handles=[p1, p2, p3]) |
54 | 45 |
|
|
0 commit comments