8000 Reimplement NonUniformImage, PcolorImage in Python, not C. · matplotlib/matplotlib@6b6a4e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b6a4e0

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 667a100 commit 6b6a4e0

File tree

5 files changed

+29
-417
lines changed

5 files changed

+29
-417
lines changed

lib/matplotlib/image.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -997,10 +997,26 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
997997
height = (round(t) + 0.5) - (round(b) - 0.5)
998998
width *= magnification
999999
height *= magnification
1000-
im = _image.pcolor(self._Ax, self._Ay, A,
1001-
int(height), int(width),
1002-
(x0, x0+v_width, y0, y0+v_height),
1003-
_interpd_[self._interpolation])
1000+
x_pix = np.linspace(x0, x0 + v_width, int(width))
1001+
y_pix = np.linspace(y0, y0 + v_height, int(height))
1002+
if self._interpolation == "nearest":
1003+
x_mid = (self._Ax[:-1] + self._Ax[1:]) / 2
1004+
y_mid = (self._Ay[:-1] + self._Ay[1:]) / 2
1005+
x_int = x_mid.searchsorted(x_pix)
1006+
y_int = y_mid.searchsorted(y_pix)
1007+
im = A[y_int[:, None], x_int[None, :]]
1008+
else: # self._interpolation == "bilinear"
1009+
x_int = np.maximum(self._Ax.searchsorted(x_pix), 1) - 1
1010+
x_frac = (x_pix - self._Ax[x_int]) / np.diff(self._Ax)[x_int]
1011+
y_int = np.maximum(self._Ax.searchsorted(y_pix), 1) - 1
1012+
y_frac = (y_pix - self._Ay[y_int]) / np.diff(self._Ay)[y_int]
1013+
x_int, y_int = np.meshgrid(x_int, y_int, indexing="xy")
1014+
x_frac = x_frac[None, :, None]
1015+
y_frac = y_frac[:, None, None]
1016+
im = ((1 - y_frac) * (1 - x_frac) * A[y_int, x_int]
1017+
+ y_frac * (1 - x_frac) * A[y_int + 1, x_int]
1018+
+ (1 - y_frac) * x_frac * A[y_int, x_int + 1]
1019+
+ y_frac * x_frac * A[y_int + 1, x_int + 1])
10041020
return im, l, b, IdentityTransform()
10051021

10061022
def set_data(self, x, y, A):
@@ -1123,11 +1139,15 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
11231139
else:
11241140
A = self._rgbacache
11251141
vl = self.axes.viewLim
1126-
im = _image.pcolor2(self._Ax, self._Ay, A,
1127-
height,
1128-
width,
1129-
(vl.x0, vl.x1, vl.y0, vl.y1),
1130-
bg)
1142+
1143+
x_pix = np.linspace(vl.x0, vl.x1, int(width))
1144+
y_pix = np.linspace(vl.y0, vl.y1, int(height))
1145+
x_int = np.clip(self._Ax.searchsorted(x_pix) - 1, 0, len(self._Ax) - 2)
1146+
y_int = np.clip(self._Ay.searchsorted(y_pix) - 1, 0, len(self._Ay) - 2)
1147+
im = A[y_int[:, None], x_int[None, :]]
1148+
x_out = (x_pix < self._Ax[0]) | (x_pix > self._Ax[-1])
1149+
y_out = (y_pix < self._Ay[0]) | (y_pix > self._Ay[-1])
1150+
im[y_out[:, None] | x_out[None, :]] = bg
11311151
return im, l, b, IdentityTransform()
11321152

11331153
def _check_unsampled_image(self, renderer):

setupext.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,6 @@ class Image(SetupPackage):
698698

699699
def get_extension(self):
700700
sources = [
701-
'src/_image.cpp',
702701
'src/mplutils.cpp',
703702
'src/_image_wrapper.cpp',
704703
'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)
0