Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove internal use of get/set dpi #23278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/matplotlib/backends/backend_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 14 additions & 4 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -1248,6 +1248,16 @@ def test_subfigure_scatter_size():
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')
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()
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down