|
1 | 1 | import matplotlib.pyplot as plt
|
2 | 2 |
|
3 | 3 | 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(): |
7 | 7 | sp.set_visible(False)
|
8 | 8 |
|
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) |
18 | 11 |
|
19 |
| - ax.spines[direction].set_visible(True) |
| 12 | +host = fig.add_subplot(111) |
| 13 | +par1 = host.twinx() |
| 14 | +par2 = host.twinx() |
20 | 15 |
|
| 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) |
21 | 25 |
|
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") |
24 | 29 |
|
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) |
26 | 34 |
|
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") |
28 | 39 |
|
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()) |
31 | 43 |
|
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) |
35 | 49 |
|
36 |
| - plt.subplots_adjust(right=0.75) |
| 50 | +lines = [p1, p2, p3] |
37 | 51 |
|
| 52 | +host.legend(lines, [l.get_label() for l in lines]) |
38 | 53 |
|
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