Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7c5396

Browse files
authored
Merge pull request #23579 from anntzer/unpar
Remove direct manipulation of HostAxes.parasites by end users.
2 parents d226954 + a9e70ce commit f7c5396

File tree

7 files changed

+34
-22
lines changed

7 files changed

+34
-22
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``HostAxesBase.get_aux_axes`` now defaults to using the same base axes class as the host axes
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
If using an ``mpl_toolkits.axisartist``-based host Axes, the parasite Axes will
4+
also be based on ``mpl_toolkits.axisartist``. This behavior is consistent with
5+
``HostAxesBase.twin``, ``HostAxesBase.twinx``, and ``HostAxesBase.twiny``.

examples/axisartist/demo_curvelinear_grid.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def curvelinear_test2(fig):
9595
ax2 = ax1.get_aux_axes(tr)
9696
# note that ax2.transData == tr + ax1.transData
9797
# Anything you draw in ax2 will match the ticks and grids of ax1.
98-
ax1.parasites.append(ax2)
9998
ax2.plot(np.linspace(0, 30, 51), np.linspace(10, 10, 51), linewidth=2)
10099

101100
ax2.pcolor(np.linspace(0, 90, 4), np.linspace(0, 10, 4),

examples/axisartist/demo_parasite_axes.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@
1717
:doc:`/gallery/axisartist/demo_parasite_axes2` example.
1818
"""
1919

20-
from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes
20+
from mpl_toolkits.axisartist.parasite_axes import HostAxes
2121
import matplotlib.pyplot as plt
2222

2323

2424
fig = plt.figure()
2525

2626
host = fig.add_axes([0.15, 0.1, 0.65, 0.8], axes_class=HostAxes)
27-
par1 = ParasiteAxes(host, sharex=host)
28-
par2 = ParasiteAxes(host, sharex=host)
29-
host.parasites.append(par1)
30-
host.parasites.append(par2)
27+
par1 = host.get_aux_axes(viewlim_mode=None, sharex=host)
28+
par2 = host.get_aux_axes(viewlim_mode=None, sharex=host)
3129

3230
host.axis["right"].set_visible(False)
3331

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,35 @@ def __init__(self, *args, **kwargs):
9999
self.parasites = []
100100
super().__init__(*args, **kwargs)
101101

102-
def get_aux_axes(self, tr=None, viewlim_mode="equal", axes_class=Axes):
102+
def get_aux_axes(
103+
self, tr=None, viewlim_mode="equal", axes_class=None, **kwargs):
103104
"""
104105
Add a parasite axes to this host.
105106
106107
Despite this method's name, this should actually be thought of as an
107108
``add_parasite_axes`` method.
108109
109-
*tr* may be `.Transform`, in which case the following relation will
110-
hold: ``parasite.transData = tr + host.transData``. Alternatively, it
111-
may be None (the default), no special relationship will hold between
112-
the parasite's and the host's ``transData``.
110+
Parameters
111+
----------
112+
tr : `.Transform` or None, default: None
113+
If a `.Transform`, the following relation will hold:
114+
``parasite.transData = tr + host.transData``.
115+
If None, the parasite's and the host's ``transData`` are unrelated.
116+
viewlim_mode : {"equal", "transform", None}, default: "equal"
117+
How the parasite's view limits are set: directly equal to the
118+
parent axes ("equal"), equal after application of *tr*
119+
("transform"), or independently (None).
120+
axes_class : subclass type of `~.axes.Axes`, optional
121+
The `.axes.Axes` subclass that is instantiated. If None, the base
122+
class of the host axes is used.
123+
kwargs
124+
Other parameters are forwarded to the parasite axes constructor.
113125
"""
126+
if axes_class is None:
127+
axes_class = self._base_axes_class
114128
parasite_axes_class = parasite_axes_class_factory(axes_class)
115-
ax2 = parasite_axes_class(self, tr, viewlim_mode=viewlim_mode)
129+
ax2 = parasite_axes_class(
130+
self, tr, viewlim_mode=viewlim_mode, **kwargs)
116131
# note that ax2.transData == tr + ax1.transData
117132
# Anything you draw in ax2 will match the ticks and grids of ax1.
118133
self.parasites.append(ax2)

lib/mpl_toolkits/tests/test_axisartist_axislines.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from matplotlib.transforms import IdentityTransform
55

66
from mpl_toolkits.axisartist.axislines import SubplotZero, Subplot
7-
from mpl_toolkits.axisartist import Axes, SubplotHost, ParasiteAxes
7+
from mpl_toolkits.axisartist import Axes, SubplotHost
88

99

1010
@image_comparison(['SubplotZero.png'], style='default')
@@ -81,8 +81,7 @@ def test_ParasiteAxesAuxTrans():
8181
ax1 = SubplotHost(fig, 1, 3, i+1)
8282
fig.add_subplot(ax1)
8383

84-
ax2 = ParasiteAxes(ax1, IdentityTransform())
85-
ax1.parasites.append(ax2)
84+
ax2 = ax1.get_aux_axes(IdentityTransform(), viewlim_mode=None)
8685
if name.startswith('pcolor'):
8786
getattr(ax2, name)(xx, yy, data[:-1, :-1])
8887
else:

lib/mpl_toolkits/tests/test_axisartist_grid_helper_curvelinear.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from matplotlib.transforms import Affine2D, Transform
77
from matplotlib.testing.decorators import image_comparison
88

9-
from mpl_toolkits.axes_grid1.parasite_axes import ParasiteAxes
109
from mpl_toolkits.axisartist import SubplotHost
1110
from mpl_toolkits.axes_grid1.parasite_axes import host_subplot_class_factory
1211
from mpl_toolkits.axisartist import angle_helper
@@ -66,8 +65,7 @@ def inverted(self):
6665
ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)
6766
fig.add_subplot(ax1)
6867

69-
ax2 = ParasiteAxes(ax1, tr, viewlim_mode="equal")
70-
ax1.parasites.append(ax2)
68+
ax2 = ax1.get_aux_axes(tr, viewlim_mode="equal")
7169
ax2.plot([3, 6], [5.0, 10.])
7270

7371
ax1.set_aspect(1.)
@@ -127,10 +125,9 @@ def test_polar_box():
127125
axis.get_helper().set_extremes(-180, 90)
128126

129127
# A parasite axes with given transform
130-
ax2 = ParasiteAxes(ax1, tr, viewlim_mode="equal")
128+
ax2 = ax1.get_aux_axes(tr, viewlim_mode="equal")
131129
assert ax2.transData == tr + ax1.transData
132130
# Anything you draw in ax2 will match the ticks and grids of ax1.
133-
ax1.parasites.append(ax2)
134131
ax2.plot(np.linspace(0, 30, 50), np.linspace(10, 10, 50))
135132

136133
ax1.set_aspect(1.)

tutorials/toolkits/axisartist.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,9 @@ def inv_tr(x, y):
519519
ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper)
520520
521521
# A parasite axes with given transform
522-
ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
522+
ax2 = ax1.get_aux_axes(tr, "equal")
523523
# note that ax2.transData == tr + ax1.transData
524524
# Anything you draw in ax2 will match the ticks and grids of ax1.
525-
ax1.parasites.append(ax2)
526525
527526
.. figure:: ../../gallery/axisartist/images/sphx_glr_demo_curvelinear_grid_001.png
528527
:target: ../../gallery/axisartist/demo_curvelinear_grid.html

0 commit comments

Comments
 (0)