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

Skip to content

Commit b52541d

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

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/matplotlib/image.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ def set_cmap(self, cmap):
897897
super(NonUniformImage, self).set_cmap(cmap)
898898

899899

900+
900901
class PcolorImage(AxesImage):
901902
"""
902903
Make a pcolor-style plot with an irregular rectangular grid.
@@ -985,6 +986,15 @@ def set_data(self, x, y, A):
985986
self.is_grayscale = True
986987
else:
987988
raise ValueError("3D arrays must have RGB or RGBA as last dim")
989+
990+
# For efficient cursor readout, ensure x and y are increasing.
991+
if x[-1] < x[0]:
992+
x = x[::-1]
993+
A = A[:, ::-1]
994+
if y[-1] < y[0]:
995+
y = y[::-1]
996+
A = A[::-1]
997+
988998
self._A = A
989999
self._Ax = x
9901000
self._Ay = y
@@ -994,6 +1004,19 @@ def set_data(self, x, y, A):
9941004
def set_array(self, *args):
9951005
raise NotImplementedError('Method not supported')
9961006

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

9981021
class FigureImage(_ImageBase):
9991022
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)