Skip to content

Commit 9831cc9

Browse files
committed
revise multiple_yaxis_with_spines.py as suggested by Stan West
svn path=/trunk/matplotlib/; revision=8740
1 parent c1f5299 commit 9831cc9

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed
Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,54 @@
11
import matplotlib.pyplot as plt
22

33
def make_patch_spines_invisible(ax):
4-
par2.set_frame_on(True)
5-
par2.patch.set_visible(False)
6-
for sp in par2.spines.itervalues():
4+
ax.set_frame_on(True)
5+
ax.patch.set_visible(False)
6+
for sp in ax.spines.itervalues():
77
sp.set_visible(False)
88

9-
def make_spine_invisible(ax, direction):
10-
if direction in ["right", "left"]:
11-
ax.yaxis.set_ticks_position(direction)
12-
ax.yaxis.set_label_position(direction)
13-
elif direction in ["top", "bottom"]:
14-
ax.xaxis.set_ticks_position(direction)
15-
ax.xaxis.set_label_position(direction)
16-
else:
17-
raise ValueError("Unknown Direction : %s" % (direction,))
9+
fig = plt.figure()
10+
fig.subplots_adjust(right=0.75)
1811

19-
ax.spines[direction].set_visible(True)
12+
host = fig.add_subplot(111)
13+
par1 = host.twinx()
14+
par2 = host.twinx()
2015

16+
# Offset the right spine of par2. The ticks and label have already been
17+
# placed on the right by twinx above.
18+
par2.spines["right"].set_position(("axes", 1.2))
19+
# Having been created by twinx, par2 has its frame off, so the line of its
20+
# detached spine is invisible. First, activate the frame but make the patch
21+
# and spines invisible.
22+
make_patch_spines_invisible(par2)
23+
# Second, show the right spine.
24+
par2.spines["right"].set_visible(True)
2125

22-
if 1:
23-
fig = plt.figure(1)
26+
p1, = host.plot([0, 1, 2], [0, 1, 2], "b-", label="Density")
27+
p2, = par1.plot([0, 1, 2], [0, 3, 2], "r-", label="Temperature")
28+
p3, = par2.plot([0, 1, 2], [50, 30, 15], "g-", label="Velocity")
2429

25-
host = fig.add_subplot(111)
30+
host.set_xlim(0, 2)
31+
host.set_ylim(0, 2)
32+
par1.set_ylim(0, 4)
33+
par2.set_ylim(1, 65)
2634

27-
host.set_xlabel("Distance")
35+
host.set_xlabel("Distance")
36+
host.set_ylabel("Density")
37+
par1.set_ylabel("Temperature")
38+
par2.set_ylabel("Velocity")
2839

29-
par1 = host.twinx()
30-
par2 = host.twinx()
40+
host.yaxis.label.set_color(p1.get_color())
41+
par1.yaxis.label.set_color(p2.get_color())
42+
par2.yaxis.label.set_color(p3.get_color())
3143

32-
par2.spines["right"].set_position(("axes", 1.2))
33-
make_patch_spines_invisible(par2)
34-
make_spine_invisible(par2, "right")
44+
tkw = dict(size=4, width=1.5)
45+
host.tick_params(axis='y', colors=p1.get_color(), **tkw)
46+
par1.tick_params(axis='y', colors=p2.get_color(), **tkw)
47+
par2.tick_params(axis='y', colors=p3.get_color(), **tkw)
48+
host.tick_params(axis='x', **tkw)
3549

36-
plt.subplots_adjust(right=0.75)
50+
lines = [p1, p2, p3]
3751

52+
host.legend(lines, [l.get_label() for l in lines])
3853

39-
p1, = host.plot([0, 1, 2], [0, 1, 2], "b-", label="Density")
40-
p2, = par1.plot([0, 1, 2], [0, 3, 2], "r-", label="Temperature")
41-
p3, = par2.plot([0, 1, 2], [50, 30, 15], "g-", label="Velocity")
42-
43-
host.set_xlim(0, 2)
44-
host.set_ylim(0, 2)
45-
par1.set_ylim(0, 4)
46-
par2.set_ylim(1, 65)
47-
48-
host.set_xlabel("Distance")
49-
host.set_ylabel("Density")
50-
par1.set_ylabel("Temperature")
51-
par2.set_ylabel("Velocity")
52-
53-
host.yaxis.label.set_color(p1.get_color())
54-
par1.yaxis.label.set_color(p2.get_color())
55-
par2.yaxis.label.set_color(p3.get_color())
56-
57-
tkw = dict(size=4, width=1.5)
58-
host.tick_params(axis='y', colors=p1.get_color(), **tkw)
59-
par1.tick_params(axis='y', colors=p2.get_color(), **tkw)
60-
par2.tick_params(axis='y', colors=p3.get_color(), **tkw)
61-
host.tick_params(axis='x', **tkw)
62-
63-
lines = [p1, p2, p3]
64-
host.legend(lines, [l.get_label() for l in lines])
65-
plt.show()
66-
54+
plt.show()

0 commit comments

Comments
 (0)