8000 Merge ParasiteAxesAuxTransBase into ParasiteAxesBase. · matplotlib/matplotlib@da2f77a · GitHub
[go: up one dir, main page]

Skip to content

Commit da2f77a

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 da2f77a

File tree

8 files changed

+109
-30
lines changed

8 files changed

+109
-30
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
18
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from matplotlib.projections import PolarAxes
18
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)
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: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
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, aux_transform=None,
14+
*, viewlim_mode=None, **kwargs):
1915
self._parent_axes = parent_axes
16+
self.transAux = aux_transform
17+
self.set_viewlim_mode(viewlim_mode)
2018
kwargs["frameon"] = False
2119
super().__init__(parent_axes.figure, parent_axes._position, **kwargs)
2220

@@ -25,6 +23,11 @@ def cla(self):
2523
martist.setp(self.get_children(), visible=False)
2624
self._get_lines = self._parent_axes._get_lines
2725

26+
def get_images_artists(self):
27+
artists = {a for a in self.get_children() if a.get_visible()}
28+
images = {a for a in self.images if a.get_visible()}
29+
return list(images), list(artists - images)
30+
2831
def pick(self, mouseevent):
2932
# This most likely goes to Artist.pick (depending on axes_class given
3033
# to the factory), which only handles pick events registered on the
@@ -37,6 +40,49 @@ def pick(self, mouseevent):
3740
and self in mouseevent.inaxes.parasites):
3841
a.pick(mouseevent)
3942

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

4187
@functools.lru_cache(None)
4288
def parasite_axes_class_factory(axes_class=None):
@@ -55,12 +101,13 @@ def parasite_axes_class_factory(axes_class=None):
55101
ParasiteAxes = parasite_axes_class_factory(Axes)
56102

57103

104+
@cbook.deprecated("3.4", alternative="ParasiteAxesBase")
58105
class ParasiteAxesAuxTransBase:
59106
def __init__(self, parent_axes, aux_transform, viewlim_mode=None,
60107
**kwargs):
61-
self.transAux = aux_transform
62-
self.set_viewlim_mode(viewlim_mode)
63-
super().__init__(parent_axes, **kwargs)
108+
# Explicit wrapper for deprecation to work.
109+
super().__init__(parent_axes, aux_transform,
110+
viewlim_mode=viewlim_mode, **kwargs)
64111

65112
def _set_lim_and_transforms(self):
66113
self.transAxes = self._parent_axes.transAxes
@@ -99,6 +146,7 @@ def apply_aspect(self, position=None):
99146
super().apply_aspect()
100147

101148

149+
@cbook.deprecated("3.4", alternative="parasite_axes_class_factory")
102150
@functools.lru_cache(None)
103151
def parasite_axes_auxtrans_class_factory(axes_class=None):
104152
if axes_class is None:
@@ -117,17 +165,30 @@ def parasite_axes_auxtrans_class_factory(axes_class=None):
117165
{'name': 'parasite_axes'})
118166

119167

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

122172

123173
class HostAxesBase:
124174
def __init__(self, *args, **kwargs):
125175
self.parasites = []
126176
super().__init__(*args, **kwargs)
127177

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

239-
parasite_axes_auxtrans_class = \
240-
parasite_axes_auxtrans_class_factory(axes_class)
300+
parasite_axes_class = parasite_axes_class_factory(axes_class)
241301

242302
if aux_trans is None:
243303
aux_trans = mtransforms.IdentityTransform()
244-
ax2 = parasite_axes_auxtrans_class(
245-
self, aux_trans, viewlim_mode="transform")
304+
ax2 = parasite_axes_class(self, aux_trans, viewlim_mode="transform")
246305
self.parasites.append(ax2)
247306
ax2._remove_method = self._remove_any_twin
248307

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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import numpy as np
2+
from matplotlib import cbook
23
import matplotlib.pyplot as plt
34
from matplotlib.testing.decorators import image_comparison
45
from matplotlib.transforms import IdentityTransform
56

67
from mpl_toolkits.axisartist.axislines import SubplotZero, Subplot
7-
from mpl_toolkits.axisartist import SubplotHost, ParasiteAxesAuxTrans
8+
from mpl_toolkits.axisartist import (
9+
Axes, SubplotHost, ParasiteAxes, ParasiteAxesAuxTrans)
810

9-
from mpl_toolkits.axisartist import Axes
11+
import pytest
1012

1113

1214
@image_comparison(['SubplotZero.png'], style='default')
@@ -59,9 +61,10 @@ def test_Axes():
5961
fig.canvas.draw()
6062

6163

64+
@pytest.mark.parametrize('parasite_cls', [ParasiteAxes, ParasiteAxesAuxTrans])
6265
@image_comparison(['ParasiteAxesAuxTrans_meshplot.png'],
6366
remove_text=True, style='default', tol=0.075)
64-
def test_ParasiteAxesAuxTrans():
67+
def test_ParasiteAxesAuxTrans(parasite_cls):
6568
# Remove this line when this test image is regenerated.
6669
plt.rcParams['pcolormesh.snap'] = False
6770

@@ -83,7 +86,8 @@ def test_ParasiteAxesAuxTrans():
8386
ax1 = SubplotHost(fig, 1, 3, i+1)
8487
fig.add_subplot(ax1)
8588

86-
ax2 = ParasiteAxesAuxTrans(ax1, IdentityTransform())
89+
with cbook._suppress_matplotlib_deprecation_warning():
90+
ax2 = parasite_cls(ax1, IdentityTransform())
8791
ax1.parasites.append(ax2)
8892
if name.startswith('pcolor'):
8993
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, viewlim_mode="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, viewlim_mode="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)
0