8000 BUG: PcolorImage handles non-contiguous arrays, provides data readout · matplotlib/matplotlib@ebfe81d · GitHub
[go: up one dir, main page]

Skip to content

Commit ebfe81d

Browse files
committed
BUG: PcolorImage handles non-contiguous arrays, provides data readout
1 parent 888bf17 commit ebfe81d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/matplotlib/image.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,15 @@ def set_data(self, x, y, A):
985985
self.is_grayscale = True
986986
else:
987987
raise ValueError("3D arrays must have RGB or RGBA as last dim")
988+
989+
# For efficient cursor readout, ensure x and y are increasing.
990+
if x[-1] < x[0]:
991+
x = x[::-1]
992+
A = A[:, ::-1]
993+
if y[-1] < y[0]:
994+
y = y[::-1]
995+
A = A[::-1]
996+
988997
self._A = A
989998
self._Ax = x
990999
self._Ay = y
@@ -994,6 +1003,19 @@ def set_data(self, x, y, A):
9941003
def set_array(self, *args):
9951004
raise NotImplementedError('Method not supported')
9961005

1006+
def get_cursor_data(self, event):
1007+
"""Get the cursor data for a given event"""
1008+
x, y = event.xdata, event.ydata
1009+
if (x < self._Ax[0] or x > self._Ax[-1] or
1010+
y < self._Ay[0] or y > self._Ay[-1]):
1011+
return None
1012+
j = np.searchsorted(self._Ax, x) - 1
1013+
i = np.searchsorted(self._Ay, y) - 1
1014+
try:
1015+
return self._A[i, j]
1016+
except:
1017+
return None
1018+
9971019

9981020
class FigureImage(_ImageBase):
9991021
zorder = 0

src/_image_wrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@ static PyObject *image_pcolor2(PyObject *self, PyObject *args, PyObject *kwds)
411411

412412
if (!PyArg_ParseTuple(args,
413413
"O&O&O&II(ffff)O&:pcolor2",
414-
&x.converter,
414+
&x.converter_contiguous,
415415
&x,
416-
&y.converter,
416+
&y.converter_contiguous,
417417
&y,
418418
&d.converter_contiguous,
419419
&d,

0 commit comments

Comments
 (0)
0