GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fixed inaccurate image placement and even more resampling bugs by ayshih · Pull Request #31021 · matplotlib/matplotlib

Fixed inaccurate image placement and even more resampling bugs #31021

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.

Sign up for GitHub

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

Merged
QuLogic merged 9 commits into matplotlib:main from ayshih:make_image_fix
Mar 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Fixed inaccurate image placement and even more resampling bugs #31021

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
9 commits
Select commit Hold shift + click to select a range
89bbd1c
Fix making an image that is to be placed at a fractional pixel
ayshih Jan 22, 2026
0ecf660
Fix affine resampling when using nearest-neighbor interpolation
ayshih Jan 29, 2026
d9d7d15
Fix the handling of image resampling beyond the image bounds
ayshih Jan 29, 2026
2d37c64
Fix pixel alignment for PcolorImage and NonUniformImage
ayshih Jan 30, 2026
dd24e05
Switch to premultiplied space inside Agg for RGBA blending
ayshih Feb 3, 2026
35ccd4c
Adjust to favor the left input pixel for nearest-neighbor interpolation
ayshih Feb 4, 2026
f1a1fb4
Bump rendering edges to fill axes when rendering affine non-NN interp…
ayshih Feb 6, 2026
ae239ea
Add four figure tests and changelog entry
ayshih Jan 28, 2026
800185c
Adjust existing figure test and update baseline images
ayshih Feb 3, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

7 changes: 7 additions & 0 deletions 7 doc/api/next_api_changes/behavior/31021-AYS.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Rendering of images now more accurate
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There have been several fixes to improve the accuracy of how images are
resampled and placed during rendering. Some inaccuracies were up to a pixel off
in the output. The most apparent improvement is that the alignment of data
pixels with tick marks and grid lines is now reliable. Nearly all image output
has changed, but often only at a subtle level that is not obvious qualitatively.
101 changes: 56 additions & 45 deletions 101 lib/matplotlib/image.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
operations.
"""

import math
import os
import logging
from pathlib import Path
Expand Down Expand Up @@ -207,12 +206,24 @@ def _resample(
out = np.zeros(out_shape + data.shape[2:], data.dtype) # 2D->2D, 3D->3D.
if resample is None:
resample = image_obj.get_resample()

# When an output pixel falls exactly on the edge between two input pixels, the Agg
# resampler will use the right input pixel as the nearest neighbor. We want the
# left input pixel to be chosen instead, so we flip the supplied transform.
if interpolation == 'nearest':
transform += Affine2D().translate(-out.shape[1], -out.shape[0]).scale(-1, -1)

_image.resample(data, out, transform,
_interpd_[interpolation],
resample,
alpha,
image_obj.get_filternorm(),
image_obj.get_filterrad())

# Because we flipped the supplied transform, we then flip the output image back.
if interpolation == 'nearest':
out = np.flip(out, axis=(0, 1))

return out


Expand Down Expand Up @@ -393,10 +404,15 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
if clipped_bbox is None:
return None, 0, 0, None

out_width_base = clipped_bbox.width * magnification
out_height_base = clipped_bbox.height * magnification
# Define the magnified bbox after clipping
magnified_extents = clipped_bbox.extents * magnification
if ((not unsampled) and round_to_pixel_border):
# Round to the nearest output pixel
magnified_bbox = Bbox.from_extents((magnified_extents + 0.5).astype(int))
else:
magnified_bbox = Bbox.from_extents(magnified_extents)

if out_width_base == 0 or out_height_base == 0:
if magnified_bbox.width == 0 or magnified_bbox.height == 0:
return None, 0, 0, None

if self.origin == 'upper':
Expand All @@ -417,23 +433,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,

t = (t0
+ (Affine2D()
.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)
else:
out_width = int(out_width_base)
out_height = int(out_height_base)
out_shape = (out_height, out_width)
.scale(magnification)
.translate(-magnified_bbox.x0, -magnified_bbox.y0)))

out_shape = (int(magnified_bbox.height), int(magnified_bbox.width))

if not unsampled:
if not (A.ndim == 2 or A.ndim == 3 and A.shape[-1] in (3, 4)):
Expand Down Expand Up @@ -560,7 +563,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
t = Affine2D().translate(
int(max(subset.xmin, 0)), int(max(subset.ymin, 0))) + t

return output, clipped_bbox.x0, clipped_bbox.y0, t
return (output,
magnified_bbox.x0 / magnification,
magnified_bbox.y0 / magnification,
t)

def make_image(self, renderer, magnification=1.0, unsampled=False):
"""
Expand Down Expand Up @@ -1061,21 +1067,24 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
B[:, :, 0:3] = A
B[:, :, 3] = 255
A = B
l, b, r, t = self.axes.bbox.extents
width = int(((round(r) + 0.5) - (round(l) - 0.5)) * magnification)
height = int(((round(t) + 0.5) - (round(b) - 0.5)) * magnification)
magnified_extents = (self.axes.bbox.extents * magnification + 0.5).astype(int)
l, b, r, t = magnified_extents / magnification
width = int((r - l) * magnification)
height = int((t - b) * magnification)

invertedTransform = self.axes.transData.inverted()
x_pix = invertedTransform.transform(
[(x, b) for x in np.linspace(l, r, width)])[:, 0]
y_pix = invertedTransform.transform(
[(l, y) for y in np.linspace(b, t, height)])[:, 1]
x_pix_edges = invertedTransform.transform(
[(x, b) for x in np.linspace(l, r, width + 1)])[:, 0]
y_pix_edges = invertedTransform.transform(
[(l, y) for y in np.linspace(b, t, height + 1)])[:, 1]
x_pix_centers = (x_pix_edges[:-1] + x_pix_edges[1:]) / 2
y_pix_centers = (y_pix_edges[:-1] + y_pix_edges[1:]) / 2

if self._interpolation == "nearest":
x_mid = (self._Ax[:-1] + self._Ax[1:]) / 2
y_mid = (self._Ay[:-1] + self._Ay[1:]) / 2
x_int = x_mid.searchsorted(x_pix)
y_int = y_mid.searchsorted(y_pix)
x_int = x_mid.searchsorted(x_pix_centers)
y_int = y_mid.searchsorted(y_pix_centers)
# The following is equal to `A[y_int[:, None], x_int[None, :]]`,
# but many times faster. Both casting to uint32 (to have an
# effectively 1D array) and manual index flattening matter.
Expand All @@ -1086,16 +1095,16 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
else: # self._interpolation == "bilinear"
# Use np.interp to compute x_int/x_float has similar speed.
x_int = np.clip(
self._Ax.searchsorted(x_pix) - 1, 0, len(self._Ax) - 2)
self._Ax.searchsorted(x_pix_centers) - 1, 0, len(self._Ax) - 2)
y_int = np.clip(
self._Ay.searchsorted(y_pix) - 1, 0, len(self._Ay) - 2)
self._Ay.searchsorted(y_pix_centers) - 1, 0, len(self._Ay) - 2)
idx_int = np.add.outer(y_int * A.shape[1], x_int)
x_frac = np.clip(
np.divide(x_pix - self._Ax[x_int], np.diff(self._Ax)[x_int],
np.divide(x_pix_centers - self._Ax[x_int], np.diff(self._Ax)[x_int],
dtype=np.float32), # Downcasting helps with speed.
0, 1)
y_frac = np.clip(
np.divide(y_pix - self._Ay[y_int], np.diff(self._Ay)[y_int],
np.divide(y_pix_centers - self._Ay[y_int], np.diff(self._Ay)[y_int],
dtype=np.float32),
0, 1)
f00 = np.outer(1 - y_frac, 1 - x_frac)
Expand Down Expand Up @@ -1248,22 +1257,24 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
if (padded_A[0, 0] != bg).all():
padded_A[[0, -1], :] = padded_A[:, [0, -1]] = bg

l, b, r, t = self.axes.bbox.extents
width = (round(r) + 0.5) - (round(l) - 0.5)
height = (round(t) + 0.5) - (round(b) - 0.5)
width = round(width * magnification)
height = round(height * magnification)
# Round to the nearest output pixels after magnification
l, b, r, t = (self.axes.bbox.extents * magnification + 0.5).astype(int)
width = r - l
height = t - b

vl = self.axes.viewLim

x_pix = np.linspace(vl.x0, vl.x1, width)
y_pix = np.linspace(vl.y0, vl.y1, height)
x_int = self._Ax.searchsorted(x_pix)
y_int = self._Ay.searchsorted(y_pix)
x_pix_edges = np.linspace(vl.x0, vl.x1, width + 1)
y_pix_edges = np.linspace(vl.y0, vl.y1, height + 1)
x_pix_centers = (x_pix_edges[:-1] + x_pix_edges[1:]) / 2
y_pix_centers = (y_pix_edges[:-1] + y_pix_edges[1:]) / 2
x_int = self._Ax.searchsorted(x_pix_centers)
y_int = self._Ay.searchsorted(y_pix_centers)
im = ( # See comment in NonUniformImage.make_image re: performance.
padded_A.view(np.uint32).ravel()[
np.add.outer(y_int * padded_A.shape[1], x_int)]
.view(np.uint8).reshape((height, width, 4)))
return im, l, b, IdentityTransform()
return im, l / magnification, b / magnification, IdentityTransform()

def _check_unsampled_image(self):
return False
Expand Down
Binary file modified BIN +486 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/extent_units.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +46 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/imshow.pdf
Show comments
View file
Open in desktop
Binary file not shown.
Binary file modified BIN -45 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/imshow.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 17 additions & 17 deletions 34 lib/matplotlib/tests/baseline_images/test_axes/imshow.svg
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -3 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/imshow_clip.pdf
Show comments
View file
Open in desktop
Binary file not shown.
Binary file modified BIN -56 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/imshow_clip.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 19 additions & 19 deletions 38 lib/matplotlib/tests/baseline_images/test_axes/imshow_clip.svg
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -1.11 KB (99%) lib/matplotlib/tests/baseline_images/test_axes/specgram_angle_freqs.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -138 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/specgram_angle_noise.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -572 Bytes (99%) lib/matplotlib/tests/baseline_images/test_axes/specgram_freqs.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -572 Bytes (99%) lib/matplotlib/tests/baseline_images/test_axes/specgram_freqs_linear.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +338 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/specgram_magnitude_freqs.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +338 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/specgram_magnitude_freqs_linear.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -295 Bytes (99%) lib/matplotlib/tests/baseline_images/test_axes/specgram_magnitude_noise.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -295 Bytes (99%) lib/matplotlib/tests/baseline_images/test_axes/specgram_magnitude_noise_linear.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -345 Bytes (99%) lib/matplotlib/tests/baseline_images/test_axes/specgram_noise.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -345 Bytes (99%) lib/matplotlib/tests/baseline_images/test_axes/specgram_noise_linear.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -509 Bytes (99%) lib/matplotlib/tests/baseline_images/test_axes/specgram_phase_freqs.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -8 Bytes (100%) lib/matplotlib/tests/baseline_images/test_axes/specgram_phase_noise.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -908 Bytes (97%) lib/matplotlib/tests/baseline_images/test_axes/use_colorizer_keyword.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +7 Bytes (100%) lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_inset_rasterized.pdf
Show comments
View file
Open in desktop
Binary file not shown.
Binary file modified BIN +3 Bytes (100%) lib/matplotlib/tests/baseline_images/test_colorbar/colorbar_extend_alpha.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN -12 Bytes (100%) lib/matplotlib/tests/baseline_images/test_colorbar/colorbar_keeping_xlabel.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +999 Bytes (100%) lib/matplotlib/tests/baseline_images/test_colors/light_source_shading_topo.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +46 Bytes (100%) lib/matplotlib/tests/baseline_images/test_colors/test_norm_abc.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +426 Bytes (120%) ...lib/tests/baseline_images/test_constrainedlayout/test_colorbars_no_overlapH.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +919 Bytes (130%) ...lib/tests/baseline_images/test_constrainedlayout/test_colorbars_no_overlapV.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +181 Bytes (110%) ...ts/baseline_images/test_constrainedlayout/test_compressed_suptitle_colorbar.png
Show comments
View file
Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BIN +115 Bytes (100%) ...s/baseline_images/test_constrainedlayout/test_compressed_supylabel_colorbar.png
Show comments
View file
Open in desktop
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Binary file added BIN +84.8 KB lib/matplotlib/tests/baseline_images/test_image/affine_fill_to_edges.png
Show comments
View file
Open in desktop
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Binary file modified BIN +27 Bytes (100%) lib/matplotlib/tests/baseline_images/test_image/bbox_image_inverted.pdf
Show comments
View file
Open in desktop
Binary file not shown.
Binary file modified BIN -42 Bytes (99%) lib/matplotlib/tests/baseline_images/test_image/bbox_image_inverted.png
Show comments
View file
Open in desktop
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.