Skip to content

Merge ParasiteAxesAuxTransBase into ParasiteAxesBase. #18675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/api/next_api_changes/deprecations/18675-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ParasiteAxesAuxTransBase
~~~~~~~~~~~~~~~~~~~~~~~~
The functionality of that mixin class has been moved to the base
``ParasiteAxesBase`` class. Thus, ``ParasiteAxesAuxTransBase``,
``ParasiteAxesAuxTrans``, and ``parasite_axes_auxtrans_class_factory`` are
deprecated.

In general, it is suggested to use ``HostAxes.get_aux_axes`` to create
parasite axes, as this saves the need of manually appending the parasite
to ``host.parasites`` and makes sure that their ``remove()`` method works
properly.
5 changes: 2 additions & 3 deletions examples/axisartist/demo_curvelinear_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D

from mpl_toolkits.axisartist import (
angle_helper, Axes, HostAxes, ParasiteAxesAuxTrans)
from mpl_toolkits.axisartist import angle_helper, Axes, HostAxes
from mpl_toolkits.axisartist.grid_helper_curvelinear import (
GridHelperCurveLinear)

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

# A parasite axes with given transform
ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
ax2 = ax1.get_aux_axes(tr)
# note that ax2.transData == tr + ax1.transData
# Anything you draw in ax2 will match the ticks and grids of ax1.
ax1.parasites.append(ax2)
Expand Down
4 changes: 3 additions & 1 deletion lib/mpl_toolkits/axes_grid/parasite_axes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from matplotlib import cbook
from mpl_toolkits.axes_grid1.parasite_axes import (
host_axes_class_factory, parasite_axes_class_factory,
parasite_axes_auxtrans_class_factory, subplot_class_factory)
from mpl_toolkits.axisartist.axislines import Axes


ParasiteAxes = parasite_axes_class_factory(Axes)
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
HostAxes = host_axes_class_factory(Axes)
SubplotHost = subplot_class_factory(HostAxes)
with cbook._suppress_matplotlib_deprecation_warning():
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
93 changes: 76 additions & 17 deletions lib/mpl_toolkits/axes_grid1/parasite_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

class ParasiteAxesBase:

def get_images_artists(self):
artists = {a for a in self.get_children() if a.get_visible()}
images = {a for a in self.images if a.get_visible()}
return list(images), list(artists - images)

def __init__(self, parent_axes, **kwargs):
def __init__(self, parent_axes, aux_transform=None,
*, viewlim_mode=None, **kwargs):
self._parent_axes = parent_axes
self.transAux = aux_transform
self.set_viewlim_mode(viewlim_mode)
kwargs["frameon"] = False
super().__init__(parent_axes.figure, parent_axes._position, **kwargs)

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

def get_images_artists(self):
artists = {a for a in self.get_children() if a.get_visible()}
images = {a for a in self.images if a.get_visible()}
return list(images), list(artists - images)

def pick(self, mouseevent):
# This most likely goes to Artist.pick (depending on axes_class given
# to the factory), which only handles pick events registered on the
Expand All @@ -37,6 +40,49 @@ def pick(self, mouseevent):
and self in mouseevent.inaxes.parasites):
a.pick(mouseevent)

# aux_transform support

def _set_lim_and_transforms(self):
if self.transAux is not None:
self.transAxes = self._parent_axes.transAxes
self.transData = self.transAux + self._parent_axes.transData
self._xaxis_transform = mtransforms.blended_transform_factory(
self.transData, self.transAxes)
self._yaxis_transform = mtransforms.blended_transform_factory(
self.transAxes, self.transData)
else:
super()._set_lim_and_transforms()

def set_viewlim_mode(self, mode):
_api.check_in_list([None, "equal", "transform"], mode=mode)
self._viewlim_mode = mode

def get_viewlim_mode(self):
return self._viewlim_mode

@cbook.deprecated("3.4", alternative="apply_aspect")
def update_viewlim(self):
return self._update_viewlim

def _update_viewlim(self): # Inline after deprecation elapses.
viewlim = self._parent_axes.viewLim.frozen()
mode = self.get_viewlim_mode()
if mode is None:
pass
elif mode == "equal":
self.axes.viewLim.set(viewlim)
elif mode == "transform":
self.axes.viewLim.set(
viewlim.transformed(self.transAux.inverted()))
else:
_api.check_in_list([None, "equal", "transform"], mode=mode)

def apply_aspect(self, position=None):
self._update_viewlim()
super().apply_aspect()

# end of aux_transform support


@functools.lru_cache(None)
def parasite_axes_class_factory(axes_class=None):
Expand All @@ -55,12 +101,13 @@ def parasite_axes_class_factory(axes_class=None):
ParasiteAxes = parasite_axes_class_factory(Axes)


@cbook.deprecated("3.4", alternative="ParasiteAxesBase")
class ParasiteAxesAuxTransBase:
def __init__(self, parent_axes, aux_transform, viewlim_mode=None,
**kwargs):
self.transAux = aux_transform
self.set_viewlim_mode(viewlim_mode)
super().__init__(parent_axes, **kwargs)
# Explicit wrapper for deprecation to work.
super().__init__(parent_axes, aux_transform,
viewlim_mode=viewlim_mode, **kwargs)

def _set_lim_and_transforms(self):
self.transAxes = self._parent_axes.transAxes
Expand Down Expand Up @@ -99,6 +146,7 @@ def apply_aspect(self, position=None):
super().apply_aspect()


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


ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
# Also deprecated.
with cbook._suppress_matplotlib_deprecation_warning():
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)


class HostAxesBase:
def __init__(self, *args, **kwargs):
self.parasites = []
super().__init__(*args, **kwargs)

def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=ParasiteAxes):
parasite_axes_class = parasite_axes_auxtrans_class_factory(axes_class)
ax2 = parasite_axes_class(self, tr, viewlim_mode)
def get_aux_axes(self, tr=None, viewlim_mode="equal", axes_class=Axes):
"""
Add a parasite axes to this host.

Despite this method's name, this should actually be thought of as an
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably add add_parasite_axes() in a followup PR and (soft-)deprecate get_aux_axes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this in another PR, as you say.

``add_parasite_axes`` method.

*tr* may be `.Transform`, in which case the following relation will
hold: ``parasite.transData = tr + host.transData``. Alternatively, it
may be None (the default), no special relationship will hold between
the parasite's and the host's ``transData``.
"""
parasite_axes_class = parasite_axes_class_factory(axes_class)
ax2 = parasite_axes_class(self, tr, viewlim_mode=viewlim_mode)
# note that ax2.transData == tr + ax1.transData
# Anything you draw in ax2 will match the ticks and grids of ax1.
self.parasites.append(ax2)
Expand Down Expand Up @@ -236,13 +297,11 @@ def twin(self, aux_trans=None, axes_class=None):
if axes_class is None:
axes_class = self._get_base_axes()

parasite_axes_auxtrans_class = \
parasite_axes_auxtrans_class_factory(axes_class)
parasite_axes_class = parasite_axes_class_factory(axes_class)

if aux_trans is None:
aux_trans = mtransforms.IdentityTransform()
ax2 = parasite_axes_auxtrans_class(
self, aux_trans, viewlim_mode="transform")
ax2 = parasite_axes_class(self, aux_trans, viewlim_mode="transform")
self.parasites.append(ax2)
ax2._remove_method = self._remove_any_twin

Expand Down
4 changes: 3 additions & 1 deletion lib/mpl_toolkits/axisartist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from matplotlib import cbook
from .axislines import (
Axes, AxesZero, AxisArtistHelper, AxisArtistHelperRectlinear,
GridHelperBase, GridHelperRectlinear, Subplot, SubplotZero)
Expand All @@ -10,6 +11,7 @@


ParasiteAxes = parasite_axes_class_factory(Axes)
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
HostAxes = host_axes_class_factory(Axes)
SubplotHost = subplot_class_factory(HostAxes)
with cbook._suppress_matplotlib_deprecation_warning():
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
4 changes: 3 additions & 1 deletion lib/mpl_toolkits/axisartist/parasite_axes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from matplotlib import cbook
from mpl_toolkits.axes_grid1.parasite_axes import (
host_axes_class_factory, parasite_axes_class_factory,
parasite_axes_auxtrans_class_factory, subplot_class_factory)
from .axislines import Axes


ParasiteAxes = parasite_axes_class_factory(Axes)
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
HostAxes = host_axes_class_factory(Axes)
SubplotHost = subplot_class_factory(HostAxes)
with cbook._suppress_matplotlib_deprecation_warning():
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(ParasiteAxes)
12 changes: 8 additions & 4 deletions lib/mpl_toolkits/tests/test_axisartist_axislines.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import numpy as np
from matplotlib import cbook
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import image_comparison
from matplotlib.transforms import IdentityTransform

from mpl_toolkits.axisartist.axislines import SubplotZero, Subplot
from mpl_toolkits.axisartist import SubplotHost, ParasiteAxesAuxTrans
from mpl_toolkits.axisartist import (
Axes, SubplotHost, ParasiteAxes, ParasiteAxesAuxTrans)

from mpl_toolkits.axisartist import Axes
import pytest


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


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

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

ax2 = ParasiteAxesAuxTrans(ax1, IdentityTransform())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is test_ParasiteAxesAuxTrans; I don't think it should be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the functionality of ParasiteAxesAuxTrans was fully folded into ParasiteAxes, so it should still be tested -- just as part of ParasiteAxes. I guess I could rename the test to test_parasite_auxtrans?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But ParasiteAxesAuxTrans has a second copy of all that code, so if it's not tested, then it could break separately from ParasiteAxes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see; parametrized the test to check both classes.
this revealed that I needed to keep the __init__ on the AuxTransBase class too, because otherwise the deprecation machinery grabs object.__init__ instead and wraps that to install the deprecation emitter.

with cbook._suppress_matplotlib_deprecation_warning():
ax2 = parasite_cls(ax1, IdentityTransform())
ax1.parasites.append(ax2)
if name.startswith('pcolor'):
getattr(ax2, name)(xx, yy, data[:-1, :-1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from matplotlib.transforms import Affine2D, Transform
from matplotlib.testing.decorators import image_comparison

from mpl_toolkits.axes_grid1.parasite_axes import ParasiteAxesAuxTrans
from mpl_toolkits.axes_grid1.parasite_axes import ParasiteAxes
from mpl_toolkits.axisartist import SubplotHost
from mpl_toolkits.axes_grid1.parasite_axes import host_subplot_class_factory
from mpl_toolkits.axisartist import angle_helper
Expand Down Expand Up @@ -68,7 +68,7 @@ def inverted(self):
ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)
fig.add_subplot(ax1)

ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
ax2 = ParasiteAxes(ax1, tr, viewlim_mode="equal")
ax1.parasites.append(ax2)
ax2.plot([3, 6], [5.0, 10.])

Expand Down Expand Up @@ -130,7 +130,7 @@ def test_polar_box():
axis.get_helper()._extremes = -180, 90

# A parasite axes with given transform
ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
ax2 = ParasiteAxes(ax1, tr, viewlim_mode="equal")
assert ax2.transData == tr + ax1.transData
# Anything you draw in ax2 will match the ticks and grids of ax1.
ax1.parasites.append(ax2)
Expand Down