|
12 | 12 | axes is shown in the :doc:`/gallery/axisartist/demo_parasite_axes` and
|
13 | 13 | :doc:`/gallery/axisartist/demo_parasite_axes2` examples.
|
14 | 14 | """
|
15 |
| -import matplotlib.pyplot as plt |
16 |
| - |
17 | 15 |
|
18 |
| -def make_patch_spines_invisible(ax): |
19 |
| - ax.set_frame_on(True) |
20 |
| - ax.patch.set_visible(False) |
21 |
| - for sp in ax.spines.values(): |
22 |
| - sp.set_visible(False) |
| 16 | +import matplotlib.pyplot as plt |
23 | 17 |
|
24 | 18 |
|
25 |
| -fig, host = plt.subplots() |
| 19 | +fig, ax = plt.subplots() |
26 | 20 | fig.subplots_adjust(right=0.75)
|
27 | 21 |
|
28 |
| -par1 = host.twinx() |
29 |
| -par2 = host.twinx() |
| 22 | +twin1 = ax.twinx() |
| 23 | +twin2 = ax.twinx() |
30 | 24 |
|
31 |
| -# Offset the right spine of par2. The ticks and label have already been |
| 25 | +# Offset the right spine of twin2. The ticks and label have already been |
32 | 26 | # placed on the right by twinx above.
|
33 |
| -par2.spines["right"].set_position(("axes", 1.2)) |
34 |
| -# Having been created by twinx, par2 has its frame off, so the line of its |
35 |
| -# detached spine is invisible. First, activate the frame but make the patch |
36 |
| -# and spines invisible. |
37 |
| -make_patch_spines_invisible(par2) |
38 |
| -# Second, show the right spine. |
39 |
| -par2.spines["right"].set_visible(True) |
| 27 | +twin2.spines["right"].set_position(("axes", 1.2)) |
40 | 28 |
|
41 |
| -p1, = host.plot([0, 1, 2], [0, 1, 2], "b-", label="Density") |
42 |
| -p2, = par1.plot([0, 1, 2], [0, 3, 2], "r-", label="Temperature") |
43 |
| -p3, = par2.plot([0, 1, 2], [50, 30, 15], "g-", label="Velocity") |
| 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") |
44 | 32 |
|
45 |
| -host.set_xlim(0, 2) |
46 |
| -host.set_ylim(0, 2) |
47 |
| -par1.set_ylim(0, 4) |
48 |
| -par2.set_ylim(1, 65) |
| 33 | +ax.set_xlim(0, 2) |
| 34 | +ax.set_ylim(0, 2) |
| 35 | +twin1.set_ylim(0, 4) |
| 36 | +twin2.set_ylim(1, 65) |
49 | 37 |
|
50 |
| -host.set_xlabel("Distance") |
51 |
| -host.set_ylabel("Density") |
52 |
| -par1.set_ylabel("Temperature") |
53 |
| -par2.set_ylabel("Velocity") |
| 38 | +ax.set_xlabel("Distance") |
| 39 | +ax.set_ylabel("Density") |
| 40 | +twin1.set_ylabel("Temperature") |
| 41 | +twin2.set_ylabel("Velocity") |
54 | 42 |
|
55 |
| -host.yaxis.label.set_color(p1.get_color()) |
56 |
| -par1.yaxis.label.set_color(p2.get_color()) |
57 |
| -par2.yaxis.label.set_color(p3.get_color()) |
| 43 | +ax.yaxis.label.set_color(p1.get_color()) |
| 44 | +twin1.yaxis.label.set_color(p2.get_color()) |
| 45 | +twin2.yaxis.label.set_color(p3.get_color()) |
58 | 46 |
|
59 | 47 | tkw = dict(size=4, width=1.5)
|
60 |
| -host.tick_params(axis='y', colors=p1.get_color(), **tkw) |
61 |
| -par1.tick_params(axis='y', colors=p2.get_color(), **tkw) |
62 |
| -par2.tick_params(axis='y', colors=p3.get_color(), **tkw) |
63 |
| -host.tick_params(axis='x', **tkw) |
64 |
| - |
65 |
| -lines = [p1, p2, p3] |
| 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) |
66 | 52 |
|
67 |
| -host.legend(lines, [l.get_label() for l in lines]) |
| 53 | +ax.legend(handles=[p1, p2, p3]) |
68 | 54 |
|
69 | 55 | plt.show()
|
0 commit comments