|
| 1 | +""" |
| 2 | +Parasite axis demo |
| 3 | +
|
| 4 | +The following code is an example of a parasite axis. |
| 5 | +It aims to show a user how to plot multiple different values |
| 6 | +onto one single plot. Notice how in this example, par1 and |
| 7 | +par2 are both calling twinx meaning both are tied directly to |
| 8 | +the x-axis. From there, each of those two axis can behave |
| 9 | +separately from the each other, meaning they can take on |
| 10 | +seperate values from themselves as well as the x-axis. |
| 11 | +""" |
1 | 12 | from mpl_toolkits.axes_grid1 import host_subplot
|
2 | 13 | import mpl_toolkits.axisartist as AA
|
3 | 14 | import matplotlib.pyplot as plt
|
4 | 15 |
|
5 |
| -if 1: |
| 16 | +host = host_subplot(111, axes_class=AA.Axes) |
| 17 | +plt.subplots_adjust(right=0.75) |
6 | 18 |
|
7 |
| - host = host_subplot(111, axes_class=AA.Axes) |
8 |
| - plt.subplots_adjust(right=0.75) |
| 19 | +par1 = host.twinx() |
| 20 | +par2 = host.twinx() |
9 | 21 |
|
10 |
| - par1 = host.twinx() |
11 |
| - par2 = host.twinx() |
| 22 | +offset = 60 |
| 23 | +new_fixed_axis = par2.get_grid_helper().new_fixed_axis |
| 24 | +par2.axis["right"] = new_fixed_axis(loc="right", |
| 25 | + axes=par2, |
| 26 | + offset=(offset, 0)) |
12 | 27 |
|
13 |
| - offset = 60 |
14 |
| - new_fixed_axis = par2.get_grid_helper().new_fixed_axis |
15 |
| - par2.axis["right"] = new_fixed_axis(loc="right", |
16 |
| - axes=par2, |
17 |
| - offset=(offset, 0)) |
| 28 | +par2.axis["right"].toggle(all=True) |
18 | 29 |
|
19 |
| - par2.axis["right"].toggle(all=True) |
| 30 | +host.set_xlim(0, 2) |
| 31 | +host.set_ylim(0, 2) |
20 | 32 |
|
21 |
| - host.set_xlim(0, 2) |
22 |
| - host.set_ylim(0, 2) |
| 33 | +host.set_xlabel("Distance") |
| 34 | +host.set_ylabel("Density") |
| 35 | +par1.set_ylabel("Temperature") |
| 36 | +par2.set_ylabel("Velocity") |
23 | 37 |
|
24 |
| - host.set_xlabel("Distance") |
25 |
| - host.set_ylabel("Density") |
26 |
| - par1.set_ylabel("Temperature") |
27 |
| - par2.set_ylabel("Velocity") |
| 38 | +p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density") |
| 39 | +p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature") |
| 40 | +p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity") |
28 | 41 |
|
29 |
| - p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density") |
30 |
| - p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature") |
31 |
| - p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity") |
| 42 | +par1.set_ylim(0, 4) |
| 43 | +par2.set_ylim(1, 65) |
32 | 44 |
|
33 |
| - par1.set_ylim(0, 4) |
34 |
| - par2.set_ylim(1, 65) |
| 45 | +host.legend() |
35 | 46 |
|
36 |
| - host.legend() |
| 47 | +host.axis["left"].label.set_color(p1.get_color()) |
| 48 | +par1.axis["right"].label.set_color(p2.get_color()) |
| 49 | +par2.axis["right"].label.set_color(p3.get_color()) |
37 | 50 |
|
38 |
| - host.axis["left"].label.set_color(p1.get_color()) |
39 |
| - par1.axis["right"].label.set_color(p2.get_color()) |
40 |
| - par2.axis["right"].label.set_color(p3.get_color()) |
41 |
| - |
42 |
| - plt.draw() |
43 |
| - plt.show() |
44 |
| - |
45 |
| - #plt.savefig("Test") |
| 51 | +plt.draw() |
| 52 | +plt.show() |
0 commit comments