Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01f1a45

Browse files
committed
Reimplement NonUniformImage, PcolorImage in Python, not C.
It's much shorter... None of this has test coverage though :( -- probably needed for the PR; but one can first check that `examples/images_contours_and_fields/image_nonuniform.py` still works.
1 parent db783f7 commit 01f1a45

File tree

5 files changed

+30
-417
lines changed

5 files changed

+30
-417
lines changed

lib/matplotlib/image.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,10 +1021,27 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
10211021
height = (round(t) + 0.5) - (round(b) - 0.5)
10221022
width *= magnification
10231023
height *= magnification
1024-
im = _image.pcolor(self._Ax, self._Ay, A,
1025-
int(height), int(width),
1026-
(vl.x0, vl.x1, vl.y0, vl.y1),
1027-
_interpd_[self._interpolation])
1024+
x_pix = np.linspace(vl.x0, vl.x1, int(width))
1025+
y_pix = np.linspace(vl.y0, vl.y1, int(height))
1026+
if self._interpolation == "nearest":
1027+
x_mid = (self._Ax[:-1] + self._Ax[1:]) / 2
1028+
y_mid = (self._Ay[:-1] + self._Ay[1:]) / 2
1029+
x_int = x_mid.searchsorted(x_pix)
1030+
y_int = y_mid.searchsorted(y_pix)
1031+
im = A[y_int[:, None], x_int[None, :]]
1032+
else: # self._interpolation == "bilinear"
1033+
x_int = np.maximum(self._Ax.searchsorted(x_pix), 1) - 1
1034+
x_frac = (x_pix - self._Ax[x_int]) / np.diff(self._Ax)[x_int]
1035+
y_int = np.maximum(self._Ax.searchsorted(y_pix), 1) - 1
1036+
y_frac = (y_pix - self._Ay[y_int]) / np.diff(self._Ay)[y_int]
1037+
x_int, y_int = np.meshgrid(x_int, y_int, indexing="xy")
1038+
x_frac = x_frac[None, :, None]
1039+
y_frac = y_frac[:, None, None]
1040+
im = (((1 - y_frac) * (1 - x_frac) * A[y_int, x_int]
1041+
+ y_frac * (1 - x_frac) * A[y_int + 1, x_int]
1042+
+ (1 - y_frac) * x_frac * A[y_int, x_int + 1]
1043+
+ y_frac * x_frac * A[y_int + 1, x_int + 1])
1044+
.astype(np.uint8))
10281045
return im, l, b, IdentityTransform()
10291046

10301047
def set_data(self, x, y, A):
@@ -1164,11 +1181,15 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
11641181
else:
11651182
A = self._rgbacache
11661183
vl = self.axes.viewLim
1167-
im = _image.pcolor2(self._Ax, self._Ay, A,
1168-
height,
1169-
width,
1170-
(vl.x0, vl.x1, vl.y0, vl.y1),
1171-
bg)
1184+
1185+
x_pix = np.linspace(vl.x0, vl.x1, int(width))
1186+
y_pix = np.linspace(vl.y0, vl.y1, int(height))
1187+
x_int = np.clip(self._Ax.searchsorted(x_pix) - 1, 0, len(self._Ax) - 2)
1188+
y_int = np.clip(self._Ay.searchsorted(y_pix) - 1, 0, len(self._Ay) - 2)
1189+
im = A[y_int[:, None], x_int[None, :]]
1190+
x_out = (x_pix < self._Ax[0]) | (x_pix > self._Ax[-1])
1191+
y_out = (y_pix < self._Ay[0]) | (y_pix > self._Ay[-1])
1192+
im[y_out[:, None] | x_out[None, :]] = bg
11721193
return im, l, b, IdentityTransform()
11731194

11741195
def _check_unsampled_image(self):

setupext.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ def get_extensions(self):
363363
# image
364364
ext = Extension(
365365
"matplotlib._image", [
366-
"src/_image.cpp",
367366
"src/mplutils.cpp",
368367
"src/_image_wrapper.cpp",
369368
"src/py_converters.cpp",

src/_image.cpp

Lines changed: 0 additions & 118 deletions
This file was deleted.

src/_image.h

Lines changed: 0 additions & 198 deletions
This file was deleted.

0 commit comments

Comments
 (0)