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