|
| 1 | +import matplotlib.pyplot as plt |
| 2 | + |
| 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(): |
| 7 | + sp.set_visible(False) |
| 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,)) |
| 18 | + |
| 19 | + ax.spines[direction].set_visible(True) |
| 20 | + |
| 21 | + |
| 22 | +if 1: |
| 23 | + fig = plt.figure(1) |
| 24 | + |
| 25 | + host = fig.add_subplot(111) |
| 26 | + |
| 27 | + host.set_xlabel("Distance") |
| 28 | + |
| 29 | + par1 = host.twinx() |
| 30 | + par2 = host.twinx() |
| 31 | + |
| 32 | + par2.spines["right"].set_position(("axes", 1.2)) |
| 33 | + make_patch_spines_invisible(par2) |
| 34 | + make_spine_invisible(par2, "right") |
| 35 | + |
| 36 | + plt.subplots_adjust(right=0.75) |
| 37 | + |
| 38 | + |
| 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 | + lines = [p1, p2, p3] |
| 58 | + host.legend(lines, [l.get_label() for l in lines]) |
| 59 | + plt.draw() |
| 60 | + plt.show() |
| 61 | + |
0 commit comments