From 0c2fed10ed95d139a8a522b8ee9f85e18d6e2729 Mon Sep 17 00:00:00 2001 From: mrinalcodez <160824830+mrinalcodez@users.noreply.github.com> Date: Thu, 24 Jul 2025 19:26:42 +0000 Subject: [PATCH 1/3] PR commit Issue 30159 for fixing Axes.clear() --- lib/matplotlib/axes/_base.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index e5175ea8761c..ee52ca56a699 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -897,7 +897,7 @@ def _request_autoscale_view(self, axis="all", tight=None): Mark a single axis, or all of them, as stale wrt. autoscaling. No computation is performed until the next autoscaling; thus, separate - calls to control individual `Axis`s incur negligible performance cost. + calls to control individual axises incur negligible performance cost. Parameters ---------- @@ -1314,8 +1314,9 @@ def __clear(self): xaxis_visible = self.xaxis.get_visible() yaxis_visible = self.yaxis.get_visible() - for axis in self._axis_map.values(): - axis.clear() # Also resets the scale to linear. + for name, axis in self._axis_map.items(): + if self._shared_axes[name].get_siblings(self) == [self]: + axis.clear() # Also resets the scale to linear. for spine in self.spines.values(): spine._clear() # Use _clear to not clear Axis again @@ -1420,6 +1421,9 @@ def __clear(self): share = getattr(self, f"_share{name}") if share is not None: getattr(self, f"share{name}")(share) + elif self in self._shared_axes[name]: + # Do not mess with limits of shared axes. + continue else: # Although the scale was set to linear as part of clear, # polar requires that _set_scale is called again @@ -2185,9 +2189,9 @@ def axis(self, arg=None, /, *, emit=True, **kwargs): xlim = self.get_xlim() ylim = self.get_ylim() edge_size = max(np.diff(xlim), np.diff(ylim))[0] - self.set_xlim(xlim[0], xlim[0] + edge_size, + self.set_xlim([xlim[0], xlim[0] + edge_size], emit=emit, auto=False) - self.set_ylim(ylim[0], ylim[0] + edge_size, + self.set_ylim([ylim[0], ylim[0] + edge_size], emit=emit, auto=False) else: raise ValueError(f"Unrecognized string {arg!r} to axis; " @@ -4749,25 +4753,14 @@ def label_outer(self, remove_inner_ticks=False): self._label_outer_yaxis(skip_non_rectangular_axes=False, remove_inner_ticks=remove_inner_ticks) - def _get_subplotspec_with_optional_colorbar(self): - """ - Return the subplotspec for this Axes, except that if this Axes has been - moved to a subgridspec to make room for a colorbar, then return the - subplotspec that encloses both this Axes and the colorbar Axes. - """ - ss = self.get_subplotspec() - if any(cax.get_subplotspec() for cax in self._colorbars): - ss = ss.get_gridspec()._subplot_spec - return ss - def _label_outer_xaxis(self, *, skip_non_rectangular_axes, remove_inner_ticks=False): # see documentation in label_outer. if skip_non_rectangular_axes and not isinstance(self.patch, mpl.patches.Rectangle): return - ss = self._get_subplotspec_with_optional_colorbar() - if ss is None: + ss = self.get_subplotspec() + if not ss: return label_position = self.xaxis.get_label_position() if not ss.is_first_row(): # Remove top label/ticklabels/offsettext. @@ -4793,8 +4786,8 @@ def _label_outer_yaxis(self, *, skip_non_rectangular_axes, if skip_non_rectangular_axes and not isinstance(self.patch, mpl.patches.Rectangle): return - ss = self._get_subplotspec_with_optional_colorbar() - if ss is None: + ss = self.get_subplotspec() + if not ss: return label_position = self.yaxis.get_label_position() if not ss.is_first_col(): # Remove left label/ticklabels/offsettext. From 67715ac8cc2f84a1bd3062155d0316fed01ba482 Mon Sep 17 00:00:00 2001 From: mrinalcodez <160824830+mrinalcodez@users.noreply.github.com> Date: Fri, 25 Jul 2025 14:31:12 +0530 Subject: [PATCH 2/3] Update _base.py --- lib/matplotlib/axes/_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index ee52ca56a699..9aaef37364d1 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -897,7 +897,7 @@ def _request_autoscale_view(self, axis="all", tight=None): Mark a single axis, or all of them, as stale wrt. autoscaling. No computation is performed until the next autoscaling; thus, separate - calls to control individual axises incur negligible performance cost. + calls to control individual `Axis`'s incur negligible performance cost. Parameters ---------- From 1c62ccf868f28ff39a9d2cbed6a69f6e7649586e Mon Sep 17 00:00:00 2001 From: mrinalcodez <160824830+mrinalcodez@users.noreply.github.com> Date: Fri, 25 Jul 2025 14:40:34 +0530 Subject: [PATCH 3/3] Update _base.py --- lib/matplotlib/axes/_base.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 9aaef37364d1..535b5cfe1c7a 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -4753,14 +4753,25 @@ def label_outer(self, remove_inner_ticks=False): self._label_outer_yaxis(skip_non_rectangular_axes=False, remove_inner_ticks=remove_inner_ticks) + def _get_subplotspec_with_optional_colorbar(self): + """ + Return the subplotspec for this Axes, except that if this Axes has been + moved to a subgridspec to make room for a colorbar, then return the + subplotspec that encloses both this Axes and the colorbar Axes. + """ + ss = self.get_subplotspec() + if any(cax.get_subplotspec() for cax in self._colorbars): + ss = ss.get_gridspec()._subplot_spec + return ss + def _label_outer_xaxis(self, *, skip_non_rectangular_axes, remove_inner_ticks=False): # see documentation in label_outer. if skip_non_rectangular_axes and not isinstance(self.patch, mpl.patches.Rectangle): return - ss = self.get_subplotspec() - if not ss: + ss = self._get_subplotspec_with_optional_colorbar() + if ss is None: return label_position = self.xaxis.get_label_position() if not ss.is_first_row(): # Remove top label/ticklabels/offsettext. @@ -4786,8 +4797,8 @@ def _label_outer_yaxis(self, *, skip_non_rectangular_axes, if skip_non_rectangular_axes and not isinstance(self.patch, mpl.patches.Rectangle): return - ss = self.get_subplotspec() - if not ss: + ss = self._get_subplotspec_with_optional_colorbar() + if ss is None: return label_position = self.yaxis.get_label_position() if not ss.is_first_col(): # Remove left label/ticklabels/offsettext.