From 14711fac10f959162a054b2b3eb43fa3ef742d49 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 26 Jun 2024 11:43:43 -0400 Subject: [PATCH] Backport PR #28465: Fix pickling of SubFigures --- lib/matplotlib/figure.py | 5 ++++- lib/matplotlib/figure.pyi | 3 ++- lib/matplotlib/tests/test_pickle.py | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index e5f4bb9421cf..d75ff527a008 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2224,7 +2224,6 @@ def __init__(self, parent, subplotspec, *, self.subplotpars = parent.subplotpars self.dpi_scale_trans = parent.dpi_scale_trans self._axobservers = parent._axobservers - self.canvas = parent.canvas self.transFigure = parent.transFigure self.bbox_relative = Bbox.null() self._redo_transform_rel_fig() @@ -2241,6 +2240,10 @@ def __init__(self, parent, subplotspec, *, self._set_artist_props(self.patch) self.patch.set_antialiased(False) + @property + def canvas(self): + return self._parent.canvas + @property def dpi(self): return self._parent.dpi diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index 21de9159d56c..b079312695c1 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -263,7 +263,6 @@ class SubFigure(FigureBase): figure: Figure subplotpars: SubplotParams dpi_scale_trans: Affine2D - canvas: FigureCanvasBase transFigure: Transform bbox_relative: Bbox figbbox: BboxBase @@ -282,6 +281,8 @@ class SubFigure(FigureBase): **kwargs ) -> None: ... @property + def canvas(self) -> FigureCanvasBase: ... + @property def dpi(self) -> float: ... @dpi.setter def dpi(self, value: float) -> None: ... diff --git a/lib/matplotlib/tests/test_pickle.py b/lib/matplotlib/tests/test_pickle.py index 7e7ccc14bf8f..0cba4f392035 100644 --- a/lib/matplotlib/tests/test_pickle.py +++ b/lib/matplotlib/tests/test_pickle.py @@ -93,6 +93,11 @@ def _generate_complete_test_figure(fig_ref): plt.errorbar(x, x * -0.5, xerr=0.2, yerr=0.4, label='$-.5 x$') plt.legend(draggable=True) + # Ensure subfigure parenting works. + subfigs = fig_ref.subfigures(2) + subfigs[0].subplots(1, 2) + subfigs[1].subplots(1, 2) + fig_ref.align_ylabels() # Test handling of _align_label_groups Groupers.