Skip to content

Commit 44bffd1

Browse files
committed
Cleanup new_fixed_axis examples.
- We can call new_fixed_axis directly on the axes instance, which will forward the call to the grid_helper and also correctly set the *axes* kwarg. - In demo_parasite_axes and demo_parasite_axes2, group together the limits and labels settings, and remove some duplicates. - Reword the description of demo_parasite_axes2.
1 parent 382be60 commit 44bffd1

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
lines changed

examples/axisartist/demo_parasite_axes.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,30 @@
2929
host.parasites.append(par1)
3030
host.parasites.append(par2)
3131

32-
host.set_ylabel("Density")
33-
host.set_xlabel("Distance")
34-
3532
host.axis["right"].set_visible(False)
36-
par1.axis["right"].set_visible(True)
37-
par1.set_ylabel("Temperature")
3833

34+
par1.axis["right"].set_visible(True)
3935
par1.axis["right"].major_ticklabels.set_visible(True)
4036
par1.axis["right"].label.set_visible(True)
4137

42-
par2.set_ylabel("Velocity")
43-
offset = (60, 0)
44-
new_axisline = par2.get_grid_helper().new_fixed_axis
45-
par2.axis["right2"] = new_axisline(loc="right", axes=par2, offset=offset)
38+
par2.axis["right2"] = par2.new_fixed_axis(loc="right", offset=(60, 0))
4639

4740
fig.add_axes(host)
4841

49-
host.set_xlim(0, 2)
50-
host.set_ylim(0, 2)
51-
52-
host.set_xlabel("Distance")
53-
host.set_ylabel("Density")
54-
par1.set_ylabel("Temperature")
55-
5642
p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
5743
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
5844
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")
5945

46+
host.set_xlim(0, 2)
47+
host.set_ylim(0, 2)
6048
par1.set_ylim(0, 4)
6149
par2.set_ylim(1, 65)
6250

51+
host.set_xlabel("Distance")
52+
host.set_ylabel("Density")
53+
par1.set_ylabel("Temperature")
54+
par2.set_ylabel("Velocity")
55+
6356
host.legend()
6457

6558
host.axis["left"].label.set_color(p1.get_color())

examples/axisartist/demo_parasite_axes2.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
"""
2-
===================
3-
Demo Parasite Axes2
4-
===================
5-
2+
==================
63
Parasite axis demo
4+
==================
75
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.
1412
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`'
1614
`~mpl_toolkits.axes_grid1.parasite_axes.host_subplot` and
1715
`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
1917
`~.mpl_toolkits.axes_grid1.parasite_axes.HostAxes` and
2018
`~.mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes` is the
2119
:doc:`/gallery/axisartist/demo_parasite_axes` example.
2220
An alternative approach using the usual matplotlib subplots is shown in
2321
the :doc:`/gallery/ticks_and_spines/multiple_yaxis_with_spines` example.
2422
"""
23+
2524
from mpl_toolkits.axes_grid1 import host_subplot
2625
from mpl_toolkits import axisartist
2726
import matplotlib.pyplot as plt
@@ -32,30 +31,25 @@
3231
par1 = host.twinx()
3332
par2 = host.twinx()
3433

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))
4035

4136
par1.axis["right"].toggle(all=True)
4237
par2.axis["right"].toggle(all=True)
4338

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+
4443
host.set_xlim(0, 2)
4544
host.set_ylim(0, 2)
45+
par1.set_ylim(0, 4)
46+
par2.set_ylim(1, 65)
4647

4748
host.set_xlabel("Distance")
4849
host.set_ylabel("Density")
4950
par1.set_ylabel("Temperature")
5051
par2.set_ylabel("Velocity")
5152

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-
5953
host.legend()
6054

6155
host.axis["left"].label.set_color(p1.get_color())

examples/axisartist/simple_axisline.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
===============
55
66
"""
7-
import matplotlib.pyplot as plt
87

8+
import matplotlib.pyplot as plt
99
from mpl_toolkits.axisartist.axislines import SubplotZero
1010

1111

@@ -30,10 +30,7 @@
3030
#ax.axis["left"].label.set_text("Label Y")
3131

3232
# make new (right-side) yaxis, but with some offset
33-
offset = (20, 0)
34-
new_axisline = ax.get_grid_helper().new_fixed_axis
35-
36-
ax.axis["right2"] = new_axisline(loc="right", offset=offset, axes=ax)
33+
ax.axis["right2"] = ax.new_fixed_axis(loc="right", offset=(20, 0))
3734
ax.axis["right2"].label.set_text("Label Y2")
3835

3936
ax.plot([-2, 3, 2])

0 commit comments

Comments
 (0)