-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Fix the misplacement of rasterized object in DrawingArea in the vectorized backends
#30171
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,7 @@ | |||||||||||||||||||||
| from matplotlib.patches import ( | ||||||||||||||||||||||
| FancyBboxPatch, FancyArrowPatch, bbox_artist as mbbox_artist) | ||||||||||||||||||||||
| from matplotlib.transforms import Bbox, BboxBase, TransformedBbox | ||||||||||||||||||||||
| from matplotlib.backends.backend_mixed import MixedModeRenderer | ||||||||||||||||||||||
|
||||||||||||||||||||||
| from matplotlib.backends.backend_mixed import MixedModeRenderer |
Copilot
AI
Apr 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scaling branch should be skipped when the renderer is already rasterizing (e.g., this DrawingArea is inside a rasterized parent). In that case points_to_pixels is already in raster-DPI units, so multiplying by mag again will over-scale (dpi factors can effectively be applied twice). Consider guarding with and not renderer._rasterizing (and/or renderer._raster_depth == 0) so the correction only applies when entering rasterization from vector mode.
Copilot
AI
Apr 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code mutates dpi_transform and offset_transform and then restores them, but restoration isn’t protected against exceptions. If c.draw(renderer) raises, the transforms remain scaled and can corrupt subsequent draws/layout. Wrap the temporary scaling/restoration in a try/finally to guarantee state restoration.
| c.draw(renderer) | |
| self.dpi_transform.clear() | |
| self.dpi_transform.scale(dpi_cor) | |
| self.offset_transform.set_matrix(off_mat) | |
| try: | |
| c.draw(renderer) | |
| finally: | |
| self.dpi_transform.clear() | |
| self.dpi_transform.scale(dpi_cor) | |
| self.offset_transform.set_matrix(off_mat) |
Copilot
AI
Apr 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as in DrawingArea.draw: temporary mutation of offset_transform/ref_offset_transform should be protected with try/finally so state is restored even if c.draw(renderer) errors.
| c.draw(renderer) | |
| self.offset_transform.set_matrix(off_mat) | |
| self.ref_offset_transform.set_matrix(ref_mat) | |
| try: | |
| c.draw(renderer) | |
| finally: | |
| self.offset_transform.set_matrix(off_mat) | |
| self.ref_offset_transform.set_matrix(ref_mat) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MixedModeRenderer.get_image_magnificationcurrently returnsself.dpi / self._figdpiunconditionally. WhenMixedModeRendereris in raster mode (self._rendereris the Agg raster renderer), callers typically expect magnification to match the active raster renderer (usually 1.0) to avoid unintended extra resampling/memory use for images drawn while rasterizing. Consider returning 1.0 (or delegating to the active renderer) when rasterizing, and only returningdpi/_figdpiwhen in vector mode.