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

Skip to content

Commit 3cdef69

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 652bef3 commit 3cdef69

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 8 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
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 getattr(self, "_lastKey", None) == key)
446453
if not reuse_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