From e12db8dcf12d408cf8cc23e95ea16b99038a058a Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 09:43:36 +0100 Subject: [PATCH 1/5] Add get/set methods for DPI in SubFigure This fixes the following error: matplotlib\lib\text.py line 1489, dop = self.figure.get_dpi()/72. AttributeError: 'SubFigure' object has no attribute 'get_dpi'. Effect: in v3.5.2 it is not possible to save a figure with a subfigure to a PDF. --- lib/matplotlib/figure.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index fc98f1c4c368..290da458ceb1 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2045,6 +2045,21 @@ def dpi(self): @dpi.setter def dpi(self, value): self._parent.dpi = value + + def get_dpi(self): + """Return the resolution of the parent figure in dots-per-inch as a float.""" + return self._parent.dpi + + def set_dpi(self, val): + """ + Set the resolution of parent figure in dots-per-inch. + + Parameters + ---------- + val : float + """ + self._parent.dpi = val + self.stale = True def _get_renderer(self): return self._parent._get_renderer() From 835154a2658234c65fc507f48143132ca81312d1 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 10:13:50 +0100 Subject: [PATCH 2/5] Fix pylint err --- lib/matplotlib/figure.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 290da458ceb1..f3459e0653b2 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2045,15 +2045,17 @@ def dpi(self): @dpi.setter def dpi(self, value): self._parent.dpi = value - + def get_dpi(self): - """Return the resolution of the parent figure in dots-per-inch as a float.""" + """ + Return the resolution of the parent figure in dots-per-inch as a float. + """ return self._parent.dpi def set_dpi(self, val): """ Set the resolution of parent figure in dots-per-inch. - + Parameters ---------- val : float From 77a02ea9caa9b5c3d9cf573ef18dc58c89b89039 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 10:28:57 +0100 Subject: [PATCH 3/5] Pylint fix2 --- lib/matplotlib/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index f3459e0653b2..8cc0e02947d4 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2051,7 +2051,7 @@ def get_dpi(self): Return the resolution of the parent figure in dots-per-inch as a float. """ return self._parent.dpi - + def set_dpi(self, val): """ Set the resolution of parent figure in dots-per-inch. From 28f35fff6e48d7efac3419d342e1b42901af0b9a Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 17:19:02 +0100 Subject: [PATCH 4/5] test --- lib/matplotlib/tests/test_figure.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index c2352ae6126e..eb5ffb9ae0c9 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1108,6 +1108,14 @@ def test_subfigure_tightbbox(): fig.get_tightbbox(fig.canvas.get_renderer()).width, 8.0) +def test_subfigure_dpi(): + fig = plt.figure(dpi=100) + sub_fig = fig.subfigures() + assert sub_fig.get_dpi() == fig.get_dpi() + + sub_fig.set_dpi(200) + assert sub_fig.get_dpi() == 200 + assert fig.get_dpi() == 200 @image_comparison(['test_subfigure_ss.png'], style='mpl20', savefig_kwarg={'facecolor': 'teal'}) From 0e63d04d654101ef9556ef81b20e060a0b1bd72d Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 21:38:33 +0100 Subject: [PATCH 5/5] Update test_figure.py --- lib/matplotlib/tests/test_figure.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index eb5ffb9ae0c9..820096283111 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1108,15 +1108,17 @@ def test_subfigure_tightbbox(): fig.get_tightbbox(fig.canvas.get_renderer()).width, 8.0) + def test_subfigure_dpi(): fig = plt.figure(dpi=100) sub_fig = fig.subfigures() assert sub_fig.get_dpi() == fig.get_dpi() - + sub_fig.set_dpi(200) assert sub_fig.get_dpi() == 200 assert fig.get_dpi() == 200 + @image_comparison(['test_subfigure_ss.png'], style='mpl20', savefig_kwarg={'facecolor': 'teal'}) def test_subfigure_ss():