From 7bafb8be7c6e81180e9518a91d10da9422321a0c Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 10:24:26 +0100 Subject: [PATCH 1/9] Deprecate internal use of get/set dpi --- lib/matplotlib/backends/backend_mixed.py | 6 +++--- lib/matplotlib/backends/backend_pdf.py | 4 ++-- lib/matplotlib/backends/backend_pgf.py | 2 +- lib/matplotlib/backends/backend_ps.py | 4 ++-- lib/matplotlib/backends/backend_svg.py | 4 ++-- lib/matplotlib/tests/test_figure.py | 8 ++++---- lib/matplotlib/text.py | 4 ++-- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/backends/backend_mixed.py b/lib/matplotlib/backends/backend_mixed.py index 0e0797186d04..5fadb96a0f73 100644 --- a/lib/matplotlib/backends/backend_mixed.py +++ b/lib/matplotlib/backends/backend_mixed.py @@ -53,7 +53,7 @@ def __init__(self, figure, width, height, dpi, vector_renderer, # the figure dpi before and after the rasterization. Although # this looks ugly, I couldn't find a better solution. -JJL self.figure = figure - self._figdpi = figure.get_dpi() + self._figdpi = figure.dpi self._bbox_inches_restore = bbox_inches_restore @@ -74,7 +74,7 @@ def start_rasterizing(self): `stop_rasterizing` is called) will be drawn with the raster backend. """ # change the dpi of the figure temporarily. - self.figure.set_dpi(self.dpi) + self.figure.dpi = self.dpi if self._bbox_inches_restore: # when tight bbox is used r = process_figure_for_rasterizing(self.figure, self._bbox_inches_restore) @@ -110,7 +110,7 @@ def stop_rasterizing(self): self._raster_renderer = None # restore the figure dpi. - self.figure.set_dpi(self._figdpi) + self.figure.dpi = self._figdpi if self._bbox_inches_restore: # when tight bbox is used r = process_figure_for_rasterizing(self.figure, diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 26cf26559113..33b7473840b1 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2758,8 +2758,8 @@ def get_default_filetype(self): def print_pdf(self, filename, *, bbox_inches_restore=None, metadata=None): - dpi = self.figure.get_dpi() - self.figure.set_dpi(72) # there are 72 pdf points to an inch + dpi = self.figure.dpi + self.figure.dpi = 72 # there are 72 pdf points to an inch width, height = self.figure.get_size_inches() if isinstance(filename, PdfPages): file = filename._file diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 6b7621271783..5c4ffe9adb84 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -812,7 +812,7 @@ def _print_pgf_to_fh(self, fh, *, bbox_inches_restore=None): # get figure size in inch w, h = self.figure.get_figwidth(), self.figure.get_figheight() - dpi = self.figure.get_dpi() + dpi = self.figure.dpi # create pgfpicture environment and write the pgf code fh.write(header_text) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 9fbff692cba1..a25c03063412 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -833,8 +833,8 @@ def _print_ps( metadata=None, papertype=None, orientation='portrait', **kwargs): - dpi = self.figure.get_dpi() - self.figure.set_dpi(72) # Override the dpi kwarg + dpi = self.figure.dpi + self.figure.dpi = 72 # Override the dpi kwarg dsc_comments = {} if isinstance(outfile, (str, os.PathLike)): diff --git a/lib/matplotlib/backends/backend_svg.py b/lib/matplotlib/backends/backend_svg.py index 5c994a121b9b..6d4517afe38f 100644 --- a/lib/matplotlib/backends/backend_svg.py +++ b/lib/matplotlib/backends/backend_svg.py @@ -1335,8 +1335,8 @@ def print_svg(self, filename, *args, bbox_inches_restore=None, with cbook.open_file_cm(filename, "w", encoding="utf-8") as fh: if not cbook.file_requires_unicode(fh): fh = codecs.getwriter('utf-8')(fh) - dpi = self.figure.get_dpi() - self.figure.set_dpi(72) + dpi = self.figure.dpi + self.figure.dpi = 72 width, height = self.figure.get_size_inches() w, h = width * 72, height * 72 renderer = MixedModeRenderer( diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index c2352ae6126e..dcbc1c49d256 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -590,8 +590,8 @@ def test_invalid_layouts(): @check_figures_equal(extensions=["png", "pdf"]) def test_add_artist(fig_test, fig_ref): - fig_test.set_dpi(100) - fig_ref.set_dpi(100) + fig_test.dpi = 100 + fig_ref.dpi = 100 fig_test.subplots() l1 = plt.Line2D([.2, .7], [.7, .7], gid='l1') @@ -1222,10 +1222,10 @@ def test_subfigure_ticks(): ax2.scatter(x=[-126.5357270050049, 94.68456736755368], y=[1500, 3600]) ax3 = subfig_bl.add_subplot(gs[0, 3:14], sharey=ax1) - fig.set_dpi(120) + fig.dpi = 120 fig.draw_without_rendering() ticks120 = ax2.get_xticks() - fig.set_dpi(300) + fig.dpi = 300 fig.draw_without_rendering() ticks300 = ax2.get_xticks() np.testing.assert_allclose(ticks120, ticks300) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 1601a49d31b6..5677a8357c06 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1503,13 +1503,13 @@ def _get_xy_transform(self, renderer, s): ref_x, ref_y = xy0 if unit == "points": # dots per points - dpp = self.figure.get_dpi() / 72. + dpp = self.figure.dpi / 72. tr = Affine2D().scale(dpp) elif unit == "pixels": tr = Affine2D() elif unit == "fontsize": fontsize = self.get_size() - dpp = fontsize * self.figure.get_dpi() / 72. + dpp = fontsize * self.figure.dpi / 72. tr = Affine2D().scale(dpp) elif unit == "fraction": w, h = bbox0.size From 88d77ddd02a29ddda364dc9794fa46aaaa52a7f9 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 10:31:10 +0100 Subject: [PATCH 2/9] pylint --- lib/matplotlib/backends/backend_ps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index a25c03063412..38ce082d31af 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -834,7 +834,7 @@ def _print_ps( **kwargs): dpi = self.figure.dpi - self.figure.dpi = 72 # Override the dpi kwarg + self.figure.dpi = 72 # Override the dpi kwarg dsc_comments = {} if isinstance(outfile, (str, os.PathLike)): From 5edf4e58dac27b6a3a19a812a9deec084e5baacd Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 17:03:16 +0100 Subject: [PATCH 3/9] Add test --- lib/matplotlib/tests/test_figure.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index dcbc1c49d256..bd04275c6f94 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1247,6 +1247,11 @@ def test_subfigure_scatter_size(): ax.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s', color='r') ax.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s', color='g') +def test_subfigure_pdf(): + fig = plt.figure(layout='constrained') + fig.subfigures() + buffer = io.StringIO() + fig.savefig(buffer, format='pdf') def test_add_subplot_kwargs(): # fig.add_subplot() always creates new axes, even if axes kwargs differ. From 91dafcd4659a4d880e07bd378a45507f29732418 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 17:53:09 +0100 Subject: [PATCH 4/9] test fix --- lib/matplotlib/tests/test_figure.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index bd04275c6f94..d8f8b4b654c1 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1247,12 +1247,17 @@ def test_subfigure_scatter_size(): ax.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s', color='r') ax.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s', color='g') + def test_subfigure_pdf(): fig = plt.figure(layout='constrained') - fig.subfigures() - buffer = io.StringIO() + sub_fig = fig.subfigures() + ax = sub_fig.add_subplot(111) + b = ax.bar(1,1) + ax.bar_label(b) + buffer = io.BytesIO() fig.savefig(buffer, format='pdf') + def test_add_subplot_kwargs(): # fig.add_subplot() always creates new axes, even if axes kwargs differ. fig = plt.figure() From 8190b122565c905a097be2979e4a20609bb6ac52 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 18:40:44 +0100 Subject: [PATCH 5/9] linting fix --- lib/matplotlib/tests/test_figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index d8f8b4b654c1..5f76b4a92f5b 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1252,7 +1252,7 @@ def test_subfigure_pdf(): fig = plt.figure(layout='constrained') sub_fig = fig.subfigures() ax = sub_fig.add_subplot(111) - b = ax.bar(1,1) + b = ax.bar(1, 1) ax.bar_label(b) buffer = io.BytesIO() fig.savefig(buffer, format='pdf') From cd749d1eafb69a1f80f0cf6e887d1bf6299a3405 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 21:31:26 +0100 Subject: [PATCH 6/9] Update lib/matplotlib/text.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 5677a8357c06..fe3d4d81fb0f 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1509,7 +1509,7 @@ def _get_xy_transform(self, renderer, s): tr = Affine2D() elif unit == "fontsize": fontsize = self.get_size() - dpp = fontsize * self.figure.dpi / 72. + dpp = fontsize * self.figure.dpi / 72 tr = Affine2D().scale(dpp) elif unit == "fraction": w, h = bbox0.size From dd855f070da0dd25d56526d67b2d9a31c0ef31e6 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 21:31:37 +0100 Subject: [PATCH 7/9] Update lib/matplotlib/text.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index fe3d4d81fb0f..fa573d3a0207 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1503,7 +1503,7 @@ def _get_xy_transform(self, renderer, s): ref_x, ref_y = xy0 if unit == "points": # dots per points - dpp = self.figure.dpi / 72. + dpp = self.figure.dpi / 72 tr = Affine2D().scale(dpp) elif unit == "pixels": tr = Affine2D() From 4258160e1ee2138678a5ea9ccb25139008427ad4 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 21:31:47 +0100 Subject: [PATCH 8/9] Update lib/matplotlib/backends/backend_ps.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/backends/backend_ps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 38ce082d31af..39e8128d3902 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -834,7 +834,7 @@ def _print_ps( **kwargs): dpi = self.figure.dpi - self.figure.dpi = 72 # Override the dpi kwarg + self.figure.dpi = 72 # Override the dpi kwarg dsc_comments = {} if isinstance(outfile, (str, os.PathLike)): From 76596a45a796015a121e664b0d56b2d5d911d629 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 15 Jun 2022 21:31:59 +0100 Subject: [PATCH 9/9] Update lib/matplotlib/backends/backend_pdf.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/backends/backend_pdf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 33b7473840b1..60b383416687 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2759,7 +2759,7 @@ def print_pdf(self, filename, *, bbox_inches_restore=None, metadata=None): dpi = self.figure.dpi - self.figure.dpi = 72 # there are 72 pdf points to an inch + self.figure.dpi = 72 # there are 72 pdf points to an inch width, height = self.figure.get_size_inches() if isinstance(filename, PdfPages): file = filename._file