Skip to content
Open
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
10 changes: 6 additions & 4 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4756,13 +4756,15 @@ def twiny(self, axes_class=None, **kwargs):
ax2.yaxis.units = self.yaxis.units
return ax2

def get_shared_x_axes(self):
@classmethod
def get_shared_x_axes(cls):
"""Return an immutable view on the shared x-axes Grouper."""
return cbook.GrouperView(self._shared_axes["x"])
return cbook.GrouperView(cls._shared_axes["x"])

def get_shared_y_axes(self):
@classmethod
def get_shared_y_axes(cls):
"""Return an immutable view on the shared y-axes Grouper."""
return cbook.GrouperView(self._shared_axes["y"])
return cbook.GrouperView(cls._shared_axes["y"])

def label_outer(self, remove_inner_ticks=False):
"""
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/axes/_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,10 @@ class _AxesBase(martist.Artist):
) -> Bbox | None: ...
def twinx(self, axes_class: Axes | None = ..., **kwargs) -> Axes: ...
def twiny(self, axes_class: Axes | None = ..., **kwargs) -> Axes: ...
def get_shared_x_axes(self) -> cbook.GrouperView: ...
def get_shared_y_axes(self) -> cbook.GrouperView: ...
@classmethod
def get_shared_x_axes(cls) -> cbook.GrouperView: ...
@classmethod
def get_shared_y_axes(cls) -> cbook.GrouperView: ...
def label_outer(self, remove_inner_ticks: bool = ...) -> None: ...

# The methods underneath this line are added via the `_axis_method_wrapper` class
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def isDefault_minfmt(self, value):
self.minor._formatter_is_default = value

def _get_shared_axes(self):
"""Return Grouper of shared Axes for current axis."""
"""Return a list of shared Axes for current axis."""
return self.axes._shared_axes[
self._get_axis_name()].get_siblings(self.axes)

Expand Down
21 changes: 15 additions & 6 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""
A collection of utility functions and classes. Originally, many
(but not all) were from the Python Cookbook -- hence the name cbook.
Expand Down Expand Up @@ -886,10 +886,17 @@
for group in unique_groups.values():
yield sorted(group, key=self._ordering.__getitem__)

def get_siblings(self, a):
"""Return all of the items joined with *a*, including itself."""
def get_siblings(self, a, *, include_self=True):
"""
Return all the items joined with *a*.

*a* is included in the list if *include_self* is True.
"""
siblings = self._mapping.get(a, [a])
return sorted(siblings, key=self._ordering.get)
result = sorted(siblings, key=self._ordering.get)
if not include_self:
result.remove(a)
return result


class GrouperView:
Expand All @@ -905,11 +912,13 @@
"""
return self._grouper.joined(a, b)

def get_siblings(self, a):
def get_siblings(self, a, *, include_self=True):
"""
Return all of the items joined with *a*, including itself.
Return all the items joined with *a*.

*a* is included in the list if *include_self* is True.
"""
return self._grouper.get_siblings(a)
return self._grouper.get_siblings(a, include_self=include_self)


def simple_linear_interpolation(a, steps):
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/cbook.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ class Grouper(Generic[_T]):
def joined(self, a: _T, b: _T) -> bool: ...
def remove(self, a: _T) -> None: ...
def __iter__(self) -> Iterator[list[_T]]: ...
def get_siblings(self, a: _T) -> list[_T]: ...
def get_siblings(self, a: _T, *, include_self: bool = True) -> list[_T]: ...

class GrouperView(Generic[_T]):
def __init__(self, grouper: Grouper[_T]) -> None: ...
def __contains__(self, item: _T) -> bool: ...
def __iter__(self) -> Iterator[list[_T]]: ...
def joined(self, a: _T, b: _T) -> bool: ...
def get_siblings(self, a: _T) -> list[_T]: ...
def get_siblings(self, a: _T, *, include_self: bool = True) -> list[_T]: ...

def simple_linear_interpolation(a: ArrayLike, steps: int) -> np.ndarray: ...
def delete_masked_points(*args): ...
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""
`matplotlib.figure` implements the following classes:

Expand Down Expand Up @@ -947,7 +947,7 @@

for name in ax._axis_names: # Break link between any shared Axes
grouper = ax._shared_axes[name]
siblings = [other for other in grouper.get_siblings(ax) if other is not ax]
siblings = grouper.get_siblings(ax, include_self=False)
if not siblings: # Axes was not shared along this axis; we're done.
continue
grouper.remove(ax)
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ class Dummy:
g.join(*objs)
assert set(list(g)[0]) == set(objs)
assert set(g.get_siblings(a)) == set(objs)
assert a not in g.get_siblings(a, include_self=False)

for other in objs[1:]:
assert g.joined(a, other)
Expand Down
Loading