Skip to content

Commit 1f79f5b

Browse files
committed
Merge ParasiteAxesAuxTransBase into ParasiteAxesBase.
It doesn't warrant being a separate mixin class (cf. merging of Subplot into Axes -- flat is better than nested). Also, currently mpl_toolkits axes mixins create non-picklable Axes subclasses because they lack the same pickling machinery as the standard SubplotBase mixin. It's not too hard to also set up the same machinery for mpl_toolkits mixins (planned for a later PR), but also supporting the double-mixin case (both ParasiteAxesBase and ParasiteAxesAuxTransBase) adds unnecessary complications.
1 parent e5a6008 commit 1f79f5b

File tree

8 files changed

+100
-26
lines changed

8 files changed

+100
-26
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ParasiteAxesAuxTransBase
2+
~~~~~~~~~~~~~~~~~~~~~~~~
3+
The functionality of that mixin class has been moved to the base
4+
``ParasiteAxesBase`` class. Thus, ``ParasiteAxesAuxTransBase``,
5+
``ParasiteAxesAuxTrans``, and ``parasite_axes_auxtrans_class_factory`` are
6+
deprecated.
7+
8+
In general, it is suggested to use ``HostAxes.get_aux_axes`` to create
9+
parasite axes, as this saves the need of manually appending the parasite
10+
to ``host.parasites`` and makes sure that their ``remove()`` method works
11+
properly.

examples/axisartist/demo_curvelinear_grid.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from matplotlib.projections import PolarAxes
1818
from matplotlib.transforms import Affine2D
1919

20-
from mpl_toolkits.axisartist import (
21-
angle_helper, Axes, HostAxes, ParasiteAxesAuxTrans)
20+
from mpl_toolkits.axisartist import angle_helper, Axes, HostAxes
2221
from mpl_toolkits.axisartist.grid_helper_curvelinear import (
2322
GridHelperCurveLinear)
2423

@@ -100,7 +99,7 @@ def curvelinear_test2(fig):
10099
ax1.grid(True, zorder=0)
101100

102101
# A parasite axes with given transform
103-
ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
102+
ax2 = ax1.get_aux_axes(tr, "equal")
104103
# note that ax2.transData == tr + ax1.transData
105104
# Anything you draw in ax2 will match the ticks and grids of ax1.
106105
ax1.parasites.append(ax2)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from matplotlib import cbook
12
from mpl_toolkits.axes_grid1.parasite_axes import (
23
host_axes_class_factory, parasite_axes_class_factory,
34
parasite_axes_auxtrans_class_factory, subplot_class_factory)
45
from mpl_toolkits.axisartist.axislines import Axes
56

67

78
ParasiteAxes = parasite_axes_class_factory(Axes)
8-
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
99
HostAxes = host_axes_class_factory(Axes)
1010
SubplotHost = subplot_class_factory(HostAxes)
11+
with cbook._suppress_matplotlib_deprecation_warning():
12+
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010

1111
class ParasiteAxesBase:
1212

13-
def get_images_artists(self):
14-
artists = {a for a in self.get_children() if a.get_visible()}
15-
images = {a for a in self.images if a.get_visible()}
16-
return list(images), list(artists - images)
17-
18-
def __init__(self, parent_axes, **kwargs):
13+
def __init__(self, parent_axes,
14+
aux_transform=None, viewlim_mode=None,
15+
**kwargs):
1916
self._parent_axes = parent_axes
17+
self.transAux = aux_transform
18+
self.set_viewlim_mode(viewlim_mode)
2019
kwargs["frameon"] = False
2120
super().__init__(parent_axes.figure, parent_axes._position, **kwargs)
2221

@@ -25,6 +24,11 @@ def cla(self):
2524
martist.setp(self.get_children(), visible=False)
2625
self._get_lines = self._parent_axes._get_lines
2726

27+
def get_images_artists(self):
28+
artists = {a for a in self.get_children() if a.get_visible()}
29+
images = {a for a in self.images if a.get_visible()}
30+
return list(images), list(artists - images)
31+
2832
def pick(self, mouseevent):
2933
# This most likely goes to Artist.pick (depending on axes_class given
3034
# to the factory), which only handles pick events registered on the
@@ -37,6 +41,49 @@ def pick(self, mouseevent):
3741
and self in mouseevent.inaxes.parasites):
3842
a.pick(mouseevent)
3943

44+
# aux_transform support
45+
46+
def _set_lim_and_transforms(self):
47+
if self.transAux is not None:
48+
self.transAxes = self._parent_axes.transAxes
49+
self.transData = self.transAux + self._parent_axes.transData
50+
self._xaxis_transform = mtransforms.blended_transform_factory(
51+
self.transData, self.transAxes)
52+
self._yaxis_transform = mtransforms.blended_transform_factory(
53+
self.transAxes, self.transData)
54+
else:
55+
super()._set_lim_and_transforms()
56+
57+
def set_viewlim_mode(self, mode):
58+
_api.check_in_list([None, "equal", "transform"], mode=mode)
59+
self._viewlim_mode = mode
60+
61+
def get_viewlim_mode(self):
62+
return self._viewlim_mode
63+
64+
@cbook.deprecated("3.4", alternative="apply_aspect")
65+
def update_viewlim(self):
66+
return self._update_viewlim
67+
68+
def _update_viewlim(self): # Inline after deprecation elapses.
69+
viewlim = self._parent_axes.viewLim.frozen()
70+
mode = self.get_viewlim_mode()
71+
if mode is None:
72+
pass
73+
elif mode == "equal":
74+
self.axes.viewLim.set(viewlim)
75+
elif mode == "transform":
76+
self.axes.viewLim.set(
77+
viewlim.transformed(self.transAux.inverted()))
78+
else:
79+
_api.check_in_list([None, "equal", "transform"], mode=mode)
80+
81+
def apply_aspect(self, position=None):
82+
self._update_viewlim()
83+
super().apply_aspect()
84+
85+
# end of aux_transform support
86+
4087

4188
@functools.lru_cache(None)
4289
def parasite_axes_class_factory(axes_class=None):
@@ -55,11 +102,10 @@ def parasite_axes_class_factory(axes_class=None):
55102
ParasiteAxes = parasite_axes_class_factory(Axes)
56103

57104

105+
@cbook.deprecated("3.4", alternative="ParasiteAxesBase")
58106
class ParasiteAxesAuxTransBase:
59107
def __init__(self, parent_axes, aux_transform, viewlim_mode=None,
60108
**kwargs):
61-
self.transAux = aux_transform
62-
self.set_viewlim_mode(viewlim_mode)
63109
super().__init__(parent_axes, **kwargs)
64110

65111
def _set_lim_and_transforms(self):
@@ -99,6 +145,7 @@ def apply_aspect(self, position=None):
99145
super().apply_aspect()
100146

101147

148+
@cbook.deprecated("3.4", alternative="parasite_axes_class_factory")
102149
@functools.lru_cache(None)
103150
def parasite_axes_auxtrans_class_factory(axes_class=None):
104151
if axes_class is None:
@@ -117,16 +164,29 @@ def parasite_axes_auxtrans_class_factory(axes_class=None):
117164
{'name': 'parasite_axes'})
118165

119166

120-
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
167+
# Also deprecated.
168+
with cbook._suppress_matplotlib_deprecation_warning():
169+
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
121170

122171

123172
class HostAxesBase:
124173
def __init__(self, *args, **kwargs):
125174
self.parasites = []
126175
super().__init__(*args, **kwargs)
127176

128-
def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=ParasiteAxes):
129-
parasite_axes_class = parasite_axes_auxtrans_class_factory(axes_class)
177+
def get_aux_axes(self, tr=None, viewlim_mode="equal", axes_class=Axes):
178+
"""
179+
Add a parasite axes to this host.
180+
181+
Despite this method's name, this should actually be thought of as an
182+
``add_parasite_axes`` method.
183+
184+
*tr* may be `.Transform`, in which case the following relation will
185+
hold: ``parasite.transData = tr + host.transData``. Alternatively, it
186+
may be None (the default), no special relationship will hold between
187+
the parasite's and the host's ``transData``.
188+
"""
189+
parasite_axes_class = parasite_axes_class_factory(axes_class)
130190
ax2 = parasite_axes_class(self, tr, viewlim_mode)
131191
# note that ax2.transData == tr + ax1.transData
132192
# Anything you draw in ax2 will match the ticks and grids of ax1.
@@ -236,13 +296,11 @@ def twin(self, aux_trans=None, axes_class=None):
236296
if axes_class is None:
237297
axes_class = self._get_base_axes()
238298

239-
parasite_axes_auxtrans_class = \
240-
parasite_axes_auxtrans_class_factory(axes_class)
299+
parasite_axes_class = parasite_axes_class_factory(axes_class)
241300

242301
if aux_trans is None:
243302
aux_trans = mtransforms.IdentityTransform()
244-
ax2 = parasite_axes_auxtrans_class(
245-
self, aux_trans, viewlim_mode="transform")
303+
ax2 = parasite_axes_class(self, aux_trans, viewlim_mode="transform")
246304
self.parasites.append(ax2)
247305
ax2._remove_method = self._remove_any_twin
248306

lib/mpl_toolkits/axisartist/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from matplotlib import cbook
12
from .axislines import (
23
Axes, AxesZero, AxisArtistHelper, AxisArtistHelperRectlinear,
34
GridHelperBase, GridHelperRectlinear, Subplot, SubplotZero)
@@ -10,6 +11,7 @@
1011

1112

1213
ParasiteAxes = parasite_axes_class_factory(Axes)
13-
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
1414
HostAxes = host_axes_class_factory(Axes)
1515
SubplotHost = subplot_class_factory(HostAxes)
16+
with cbook._suppress_matplotlib_deprecation_warning():
17+
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from matplotlib import cbook
12
from mpl_toolkits.axes_grid1.parasite_axes import (
23
host_axes_class_factory, parasite_axes_class_factory,
34
parasite_axes_auxtrans_class_factory, subplot_class_factory)
45
from .axislines import Axes
56

67

78
ParasiteAxes = parasite_axes_class_factory(Axes)
8-
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
99
HostAxes = host_axes_class_factory(Axes)
1010
SubplotHost = subplot_class_factory(HostAxes)
11+
with cbook._suppress_matplotlib_deprecation_warning():
12+
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)

lib/mpl_toolkits/tests/test_axisartist_axislines.py

Lines changed: 2 additions & 2 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 SubplotHost, ParasiteAxesAuxTrans
7+
from mpl_toolkits.axisartist import SubplotHost, ParasiteAxes
88

99
from mpl_toolkits.axisartist import Axes
1010

@@ -83,7 +83,7 @@ def test_ParasiteAxesAuxTrans():
8383
ax1 = SubplotHost(fig, 1, 3, i+1)
8484
fig.add_subplot(ax1)
8585

86-
ax2 = ParasiteAxesAuxTrans(ax1, IdentityTransform())
86+
ax2 = ParasiteAxes(ax1, IdentityTransform())
8787
ax1.parasites.append(ax2)
8888
if name.startswith('pcolor'):
8989
getattr(ax2, name)(xx, yy, data[:-1, :-1])

lib/mpl_toolkits/tests/test_axisartist_grid_helper_curvelinear.py

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

10-
from mpl_toolkits.axes_grid1.parasite_axes import ParasiteAxesAuxTrans
10+
from mpl_toolkits.axes_grid1.parasite_axes import ParasiteAxes
1111
from mpl_toolkits.axisartist import SubplotHost
1212
from mpl_toolkits.axes_grid1.parasite_axes import host_subplot_class_factory
1313
from mpl_toolkits.axisartist import angle_helper
@@ -68,7 +68,7 @@ def inverted(self):
6868
ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)
6969
fig.add_subplot(ax1)
7070

71-
ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
71+
ax2 = ParasiteAxes(ax1, tr, "equal")
7272
ax1.parasites.append(ax2)
7373
ax2.plot([3, 6], [5.0, 10.])
7474

@@ -130,7 +130,7 @@ def test_polar_box():
130130
axis.get_helper()._extremes = -180, 90
131131

132132
# A parasite axes with given transform
133-
ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
133+
ax2 = ParasiteAxes(ax1, tr, "equal")
134134
assert ax2.transData == tr + ax1.transData
135135
# Anything you draw in ax2 will match the ticks and grids of ax1.
136136
ax1.parasites.append(ax2)

0 commit comments

Comments
 (0)