Skip to content

Move towards having get_shared_{x,y}_axes return immutable views. #21584

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
Jun 2, 2022
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
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/deprecations/21584-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Modifications to the Groupers returned by ``get_shared_x_axes`` and ``get_shared_y_axes``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... are deprecated. In the future, these methods will return immutable views
on the grouper structures. Note that previously, calling e.g. ``join()``
would already fail to set up the correct structures for sharing axes; use
`.Axes.sharex` or `.Axes.sharey` instead.
8 changes: 4 additions & 4 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4595,9 +4595,9 @@ def twiny(self):
return ax2

def get_shared_x_axes(self):
"""Return a reference to the shared axes Grouper object for x axes."""
return self._shared_axes["x"]
"""Return an immutable view on the shared x-axes Grouper."""
return cbook.GrouperView(self._shared_axes["x"])

def get_shared_y_axes(self):
"""Return a reference to the shared axes Grouper object for y axes."""
return self._shared_axes["y"]
"""Return an immutable view on the shared y-axes Grouper."""
return cbook.GrouperView(self._shared_axes["y"])
28 changes: 28 additions & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,34 @@ def get_siblings(self, a):
return [x() for x in siblings]


class GrouperView:
"""Immutable view over a `.Grouper`."""

def __init__(self, grouper):
self._grouper = grouper

class _GrouperMethodForwarder:
def __init__(self, deprecated_kw=None):
self._deprecated_kw = deprecated_kw

def __set_name__(self, owner, name):
wrapped = getattr(Grouper, name)
forwarder = functools.wraps(wrapped)(
lambda self, *args, **kwargs: wrapped(
self._grouper, *args, **kwargs))
if self._deprecated_kw:
forwarder = _api.deprecated(**self._deprecated_kw)(forwarder)
setattr(owner, name, forwarder)

__contains__ = _GrouperMethodForwarder()
__iter__ = _GrouperMethodForwarder()
joined = _GrouperMethodForwarder()
get_siblings = _GrouperMethodForwarder()
clean = _GrouperMethodForwarder(deprecated_kw=dict(since="3.6"))
join = _GrouperMethodForwarder(deprecated_kw=dict(since="3.6"))
remove = _GrouperMethodForwarder(deprecated_kw=dict(since="3.6"))


def simple_linear_interpolation(a, steps):
"""
Resample an array with ``steps - 1`` points between original point pairs.
Expand Down