Skip to content
Draft
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
12 changes: 12 additions & 0 deletions doc/api/api_changes/2017-03-11_TAC.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Round instead of truncating pixel number when using Agg
```````````````````````````````````````````````````````

When rasterizing a figure the allowed figure sizes are effectively
discretized by the finite dpi. Previously, we simply truncated when
computing the number of pixels to use, now we use `numpy.round` which
will avoid floating point issues when setting the size of the figure
in inches to get a specific size of in pixels.

Additionally, the Agg based backends will adjust the size of the
figure to match the actual size (given by the number of pixels and the
dpi) during the draw process.
9 changes: 8 additions & 1 deletion lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,14 @@ def draw(self):

@_api.delete_parameter("3.6", "cleared", alternative="renderer.clear()")
def get_renderer(self, cleared=False):
w, h = self.figure.bbox.size
w, h = np.round(self.figure.bbox.size).astype(int)
dpi = self.figure.dpi

# we know we are using Agg, thus are tied to discrete sizes
# set by the dpi. Feed this back so that the transforms are
# mapped to the available pixels
self.figure.set_size_inches(w / dpi, h / dpi)
Comment on lines +410 to +413
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fully trust this with the way we handle HiDPI (where figure.dpi changes based on screen)?


key = w, h, self.figure.dpi
reuse_renderer = (self._lastKey == key)
if not reuse_renderer:
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2954,10 +2954,12 @@ def set_size_inches(self, w, h=None, forward=True):
if not np.isfinite(size).all() or (size < 0).any():
raise ValueError(f'figure size must be positive finite not {size}')
self.bbox_inches.p1 = size
if tuple(size) == (w, h) and not forward:
return
if forward:
manager = self.canvas.manager
if manager is not None:
manager.resize(*(size * self.dpi).astype(int))
manager.resize(*np.round((size * self.dpi)).astype(int))
self.stale = True

def get_size_inches(self):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.