-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Fix: prevent imshow pixel overlap with grid lines #31015
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| imshow pixel alignment | ||
| ~~~~~~~~~~~~~~~~~~~~~~ | ||
| ``imshow`` now rounds the output image dimensions to the nearest integer pixel | ||
| rather than always using ``math.ceil``. This ensures that the edges of raster | ||
| pixels align correctly with vector grid lines and patches, preventing 1-pixel | ||
| overlaps or gaps when the image is small or pixels are large. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,6 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
| operations. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import math | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -420,16 +419,29 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, | |||||||||||||||||||||||||||||||||||||||||||||||||
| .translate(-clipped_bbox.x0, -clipped_bbox.y0) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| .scale(magnification))) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # So that the image is aligned with the edge of the Axes, we want to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # round up the output width to the next integer. This also means | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # scaling the transform slightly to account for the extra subpixel. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((not unsampled) and t.is_affine and round_to_pixel_border and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| (out_width_base % 1.0 != 0.0 or out_height_base % 1.0 != 0.0)): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_width = math.ceil(out_width_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_height = math.ceil(out_height_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| extra_width = (out_width - out_width_base) / out_width_base | ||||||||||||||||||||||||||||||||||||||||||||||||||
| extra_height = (out_height - out_height_base) / out_height_base | ||||||||||||||||||||||||||||||||||||||||||||||||||
| t += Affine2D().scale(1.0 + extra_width, 1.0 + extra_height) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # So that the image is aligned with the edge of the Axes, we round the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # output size to the nearest integer, and scale the transform slightly | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # to account for any resulting subpixel difference. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((not unsampled) and t.is_affine and round_to_pixel_border): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_width = round(out_width_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_height = round(out_height_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if out_width > 0 and out_height > 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| extra_width = (out_width - out_width_base) / out_width_base | ||||||||||||||||||||||||||||||||||||||||||||||||||
| extra_height = (out_height - out_height_base) / out_height_base | ||||||||||||||||||||||||||||||||||||||||||||||||||
| t += Affine2D().scale(1.0 + extra_width, 1.0 + extra_height) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Fallback for small positive base dimensions: ensure at least | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # one pixel so we do not end up with a zero-sized output. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if out_width_base > 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_width = max(1, int(out_width_base)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_width = int(out_width_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if out_height_base > 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_height = max(1, int(out_height_base)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_height = int(out_height_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+426
to
+444
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think all of the protection logic is unnecessary. Are there realistic situations when |
||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_width = int(out_width_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| out_height = int(out_height_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
For this use case, you shouldn't use
round()to round. Python'sround()(as well as NumPy'sround()) round to the nearest even value.