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

Skip to content

Commit e146733

Browse files
authored
Merge pull request #18468 from anntzer/multiy
Cleanup multiple_yaxis_with_spines example.
2 parents 7fb5b6c + bd67fb7 commit e146733

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

examples/ticks_and_spines/multiple_yaxis_with_spines.py

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,44 @@
1212
axes is shown in the :doc:`/gallery/axisartist/demo_parasite_axes` and
1313
:doc:`/gallery/axisartist/demo_parasite_axes2` examples.
1414
"""
15-
import matplotlib.pyplot as plt
16-
1715

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
2317

2418

25-
fig, host = plt.subplots()
19+
fig, ax = plt.subplots()
2620
fig.subplots_adjust(right=0.75)
2721

28-
par1 = host.twinx()
29-
par2 = host.twinx()
22+
twin1 = ax.twinx()
23+
twin2 = ax.twinx()
3024

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
3226
# 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))
4028

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")
4432

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)
4937

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")
5442

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())
5846

5947
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)
6652

67-
host.legend(lines, [l.get_label() for l in lines])
53+
ax.legend(handles=[p1, p2, p3])
6854

6955
plt.show()

0 commit comments

Comments
 (0)