8000 Merge pull request #23278 from scottjones03/main · matplotlib/matplotlib@047254d · GitHub
[go: up one dir, main page]

Skip to content

Commit 047254d

Browse files
authored
Merge pull request #23278 from scottjones03/main
FIX: Remove internal use of get_dpi/set_dpi
2 parents d417b98 + 76596a4 commit 047254d

File tree

7 files changed

+26
-16
lines changed

7 files changed

+26
-16
lines changed

lib/matplotlib/backends/backend_mixed 8000 .py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, figure, width, height, dpi, vector_renderer,
5353
# the figure dpi before and after the rasterization. Although
5454
# this looks ugly, I couldn't find a better solution. -JJL
5555
self.figure = figure
56-
self._figdpi = figure.get_dpi()
56+
self._figdpi = figure.dpi
5757

5858
self._bbox_inches_restore = bbox_inches_restore
5959

@@ -74,7 +74,7 @@ def start_rasterizing(self):
7474
`stop_rasterizing` is called) will be drawn with the raster backend.
7575
"""
7676
# change the dpi of the figure temporarily.
77-
self.figure.set_dpi(self.dpi)
77+
self.figure.dpi = self.dpi
7878
if self._bbox_inches_restore: # when tight bbox is used
7979
r = process_figure_for_rasterizing(self.figure,
8080
self._bbox_inches_restore)
@@ -110,7 +110,7 @@ def stop_rasterizing(self):
110110
self._raster_renderer = None
111111

112112
# restore the figure dpi.
113-
self.figure.set_dpi(self._figdpi)
113+
self.figure.dpi = self._figdpi
114114

115115
if self._bbox_inches_restore: # when tight bbox is used
116116
r = process_figure_for_rasterizing(self.figure,

lib/matplotlib/backends/backend_pdf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,8 +2760,8 @@ def get_default_filetype(self):
27602760
def print_pdf(self, filename, *,
27612761
bbox_inches_restore=None, metadata=None):
27622762

2763-
dpi = self.figure.get_dpi()
2764-
self.figure.set_dpi(72) # there are 72 pdf points to an inch
2763+
dpi = self.figure.dpi
2764+
self.figure.dpi = 72 # there are 72 pdf points to an inch
27652765
width, height = self.figure.get_size_inches()
27662766
if isinstance(filename, PdfPages):
27672767
file = filename._file

lib/matplotlib/backends/backend_pgf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ def _print_pgf_to_fh(self, fh, *, bbox_inches_restore=None):
812812

813813
# get figure size in inch
814814
w, h = self.figure.get_figwidth(), self.figure.get_figheight()
815-
dpi = self.figure.get_dpi()
815+
dpi = self.figure.dpi
816816

817817
# create pgfpicture environment and write the pgf code
818818
fh.write(header_text)

lib/matplotlib/backends/backend_ps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,8 @@ def _print_ps(
833833
metadata=None, papertype=None, orientation='portrait',
834834
**kwargs):
835835

836-
dpi = self.figure.get_dpi()
837-
self.figure.set_dpi(72) # Override the dpi kwarg
836+
dpi = self.figure.dpi
837+
self.figure.dpi = 72 # Override the dpi kwarg
838838

839839
dsc_comments = {}
840840
if isinstance(outfile, (str, os.PathLike)):

lib/matplotlib/backends/backend_svg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,8 @@ def print_svg(self, filename, *args, bbox_inches_restore=None,
13271327
with cbook.open_file_cm(filename, "w", encoding="utf-8") as fh:
13281328
if not cbook.file_requires_unicode(fh):
13291329
fh = codecs.getwriter('utf-8')(fh)
1330-
dpi = self.figure.get_dpi()
1331-
self.figure.set_dpi(72)
1330+
dpi = self.figure.dpi
1331+
self.figure.dpi = 72
13321332
width, height = self.figure.get_size_inches()
13331333
w, h = width * 72, height * 72
13341334
renderer = MixedModeRenderer(

lib/matplotlib/tests/test_figure.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,8 @@ def test_invalid_layouts():
590590

591591
@check_figures_equal(extensions=["png", "pdf"])
592592
def test_add_artist(fig_test, fig_ref):
593-
fig_test.set_dpi(100)
594-
fig_ref.set_dpi(100)
593+
fig_test.dpi = 100
594+
fig_ref.dpi = 100
595595

596596
fig_test.subplots()
597597
l1 = plt.Line2D([.2, .7], [.7, .7], gid='l1')
@@ -1232,10 +1232,10 @@ def test_subfigure_ticks():
12321232
ax2.scatter(x=[-126.5357270050049, 94.68456736755368], y=[1500, 3600])
12331233
ax3 = subfig_bl.add_subplot(gs[0, 3:14], sharey=ax1)
12341234

1235-
fig.set_dpi(120)
1235+
fig.dpi = 120
12361236
fig.draw_without_rendering()
12371237
ticks120 = ax2.get_xticks()
1238-
fig.set_dpi(300)
1238+
fig.dpi = 300
12391239
fig.draw_without_rendering()
12401240
ticks300 = ax2.get_xticks()
12411241
np.testing.assert_allclose(ticks120, ticks300)
@@ -1258,6 +1258,16 @@ def test_subfigure_scatter_size():
12581258
ax.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s', color='g')
12591259

12601260

1261+
def test_subfigure_pdf():
1262+
fig = plt.figure(layout='constrained')
1263+
sub_fig = fig.subfigures()
1264+
ax = sub_fig.add_subplot(111)
1265+
b = ax.bar(1, 1)
1266+
ax.bar_label(b)
1267+
buffer = io.BytesIO()
1268+
fig.savefig(buffer, format='pdf')
1269+
1270+
12611271
def test_add_subplot_kwargs():
12621272
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
12631273
fig = plt.figure()

lib/matplotlib/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,13 +1504,13 @@ def _get_xy_transform(self, renderer, s):
15041504
ref_x, ref_y = xy0
15051505
if unit == "points":
15061506
# dots per points
1507-
dpp = self.figure.get_dpi() / 72.
1507+
dpp = self.figure.dpi / 72
15081508
tr = Affine2D().scale(dpp)
15091509
elif unit == "pixels":
15101510
tr = Affine2D()
15111511
elif unit == "fontsize":
15121512
fontsize = self.get_size()
1513-
dpp = fontsize * self.figure.get_dpi() / 72.
1513+
dpp = fontsize * self.figure.dpi / 72
15141514
tr = Affine2D().scale(dpp)
15151515
elif unit == "fraction":
15161516
w, h = bbox0.size

0 commit comments

Comments
 (0)
0