Skip to content
Merged
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
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/behavior/31021-AYS.rst
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 lib/matplotlib/image.py
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 lib/matplotlib/tests/baseline_images/test_axes/extent_units.png
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 lib/matplotlib/tests/baseline_images/test_axes/imshow.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/imshow.png
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 lib/matplotlib/tests/baseline_images/test_axes/imshow.svg
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 lib/matplotlib/tests/baseline_images/test_axes/imshow_clip.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/imshow_clip.png
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 lib/matplotlib/tests/baseline_images/test_axes/imshow_clip.svg
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.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/specgram_freqs.png
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.
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 lib/matplotlib/tests/baseline_images/test_axes/specgram_noise.png
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.
Binary file not shown.
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.
Binary file modified lib/matplotlib/tests/baseline_images/test_colors/test_norm_abc.png
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.
Binary file not shown.
Loading
Loading