|
1 | 1 | """ |
2 | | -=================== |
3 | | -Demo Parasite Axes2 |
4 | | -=================== |
5 | | -
|
| 2 | +================== |
6 | 3 | Parasite axis demo |
| 4 | +================== |
| 5 | +
|
| 6 | +This example demonstrates the use of parasite axis to plot multiple datasets |
| 7 | +onto one single plot. |
7 | 8 |
|
8 | | -The following code is an example of a parasite axis. It aims to show how |
9 | | -to plot multiple different values onto one single plot. Notice how in this |
10 | | -example, par1 and par2 are both calling twinx meaning both are tied directly to |
11 | | -the x-axis. From there, each of those two axis can behave separately from the |
12 | | -each other, meaning they can take on separate values from themselves as well as |
13 | | -the x-axis. |
| 9 | +Notice how in this example, *par1* and *par2* are both obtained by calling |
| 10 | +``twinx()``, which ties their x-limits with the host's x-axis. From there, each |
| 11 | +of those two axis behave separately from each other: different datasets can be |
| 12 | +plotted, and the y-limits are adjusted separately. |
14 | 13 |
|
15 | | -Note that this approach uses the `mpl_toolkits.axes_grid1.parasite_axes`\' |
| 14 | +Note that this approach uses the `mpl_toolkits.axes_grid1.parasite_axes`' |
16 | 15 | `~mpl_toolkits.axes_grid1.parasite_axes.host_subplot` and |
17 | 16 | `mpl_toolkits.axisartist.axislines.Axes`. An alternative approach using the |
18 | | -`~mpl_toolkits.axes_grid1.parasite_axes`\'s |
| 17 | +`~mpl_toolkits.axes_grid1.parasite_axes`'s |
19 | 18 | `~.mpl_toolkits.axes_grid1.parasite_axes.HostAxes` and |
20 | 19 | `~.mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes` is the |
21 | 20 | :doc:`/gallery/axisartist/demo_parasite_axes` example. |
22 | | -An alternative approach using the usual matplotlib subplots is shown in |
| 21 | +An alternative approach using the usual Matplotlib subplots is shown in |
23 | 22 | the :doc:`/gallery/ticks_and_spines/multiple_yaxis_with_spines` example. |
24 | 23 | """ |
| 24 | + |
25 | 25 | from mpl_toolkits.axes_grid1 import host_subplot |
26 | 26 | from mpl_toolkits import axisartist |
27 | 27 | import matplotlib.pyplot as plt |
|
32 | 32 | par1 = host.twinx() |
33 | 33 | par2 = host.twinx() |
34 | 34 |
|
35 | | -offset = 60 |
36 | | -new_fixed_axis = par2.get_grid_helper().new_fixed_axis |
37 | | -par2.axis["right"] = new_fixed_axis(loc="right", |
38 | | - axes=par2, |
39 | | - offset=(offset, 0)) |
| 35 | +par2.axis["right"] = par2.new_fixed_axis(loc="right", offset=(60, 0)) |
40 | 36 |
|
41 | 37 | par1.axis["right"].toggle(all=True) |
42 | 38 | par2.axis["right"].toggle(all=True) |
43 | 39 |
|
| 40 | +p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density") |
| 41 | +p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature") |
| 42 | +p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity") |
| 43 | + |
44 | 44 | host.set_xlim(0, 2) |
45 | 45 | host.set_ylim(0, 2) |
| 46 | +par1.set_ylim(0, 4) |
| 47 | +par2.set_ylim(1, 65) |
46 | 48 |
|
47 | 49 | host.set_xlabel("Distance") |
48 | 50 | host.set_ylabel("Density") |
49 | 51 | par1.set_ylabel("Temperature") |
50 | 52 | par2.set_ylabel("Velocity") |
51 | 53 |
|
52 | | -p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density") |
53 | | -p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature") |
54 | | -p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity") |
55 | | - |
56 | | -par1.set_ylim(0, 4) |
57 | | -par2.set_ylim(1, 65) |
58 | | - |
59 | 54 | host.legend() |
60 | 55 |
|
61 | 56 | host.axis["left"].label.set_color(p1.get_color()) |
|
0 commit comments