8000 API: feedback to figure size due to pixel counts · matplotlib/matplotlib@fb247bc · GitHub
[go: up one dir, main page]

Skip to content

Commit fb247bc

Browse files
committed
API: feedback to figure size due to pixel counts
When rasterizing the figure, the allowed sizes are discrete due to a finite dpi. When getting the renderer (at which point we have committed to rasterizing the figure at this dpi) feedback the actual size.
1 parent cf64c65 commit fb247bc

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,14 @@ def draw(self):
440440

441441
def get_renderer(self, cleared=False):
442442
l, b, w, h = np.round(self.figure.bbox.size).astype(int)
443-
key = w, h, self.figure.dpi
443+
dpi = self.figure.dpi
444+
445+
# we know we are using Agg, thus are tied to discrete sizes
446+
# set by the dpi. Feed this back so that the transforms are
447+
# mapped to the available pixels
448+
self.figure.set_size_inches(w / dpi, h / dpi)
449+
450+
key = w, h, dpi
444451
reuse_renderer = (hasattr(self, "renderer")
445452
and< 8000 /span> getattr(self, "_lastKey", None) == key)
446453
if not reuse_renderer:
@@ -549,6 +556,7 @@ def print_png(self, filename_or_obj, *args,
549556
"""
550557
self._print_pil(filename_or_obj, "png", pil_kwargs, metadata)
551558

559+
552560
def print_to_buffer(self):
553561
FigureCanvasAgg.draw(self)
554562
renderer = self.get_renderer()

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,17 @@ def get_diff_image(self):
256256
return png.getvalue()
257257

258258
def get_renderer(self, cleared=None):
259-
# Mirrors super.get_renderer, but caches the old one so that we can do
260-
# things such as produce a diff image in get_diff_image.
261-
w, h = np.round(self.figure.bbox.size).astype(int)
262-
key = w, h, self.figure.dpi
259+
# Mirrors super.get_renderer, but caches the old one
260+
# so that we can do things such as produce a diff image
261+
# in get_diff_image
262+
_, _, w, h = np.round(self.figure.bbox.bounds).astype(int)
263+
dpi = self.figure.dpi
264+
# we know we are using Agg, thus are tied to discrete sizes
265+
# set by the dpi. Feed this back so that the transforms are
266+
# mapped to the available pixels
267+
self.figure.set_size_inches(w / dpi, h / dpi)
268+
269+
key = w, h, dpi
263270
try:
264271
self._lastKey, self._renderer
265272
except AttributeError:

0 commit comments

Comments
 (0)
0