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

Skip to content

Commit 28b6a99

Browse files
authored
Merge pull request #6930 from efiring/PcolorImage_cursor
BUG: PcolorImage handles non-contiguous arrays, provides data readout
2 parents ee8f448 + f1b6135 commit 28b6a99

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5519,8 +5519,9 @@ def pcolormesh(self, *args, **kwargs):
55195519
.. seealso::
55205520
55215521
:func:`~matplotlib.pyplot.pcolor`
5522-
For an explanation of the grid orientation and the
5523-
expansion of 1-D *X* and/or *Y* to 2-D arrays.
5522+
For an explanation of the grid orientation
5523+
(:ref:`Grid Orientation <axes-pcolor-grid-orientation>`)
5524+
and the expansion of 1-D *X* and/or *Y* to 2-D arrays.
55245525
"""
55255526
if not self._hold:
55265527
self.cla()
@@ -5634,10 +5635,10 @@ def pcolorfast(self, *args, **kwargs):
56345635
(*nr*-1, *nc*-1). All cells are rectangles of the same size.
56355636
This is the fastest version.
56365637
5637-
*x*, *y* are 1D arrays of length *nc* +1 and *nr* +1, respectively,
5638-
giving the x and y boundaries of the cells. Hence the cells are
5639-
rectangular but the grid may be nonuniform. The speed is
5640-
intermediate. (The grid is checked, and if found to be
5638+
*x*, *y* are monotonic 1D arrays of length *nc* +1 and *nr* +1,
5639+
respectively, giving the x and y boundaries of the cells. Hence
5640+
the cells are rectangular but the grid may be nonuniform. The
5641+
speed is intermediate. (The grid is checked, and if found to be
56415642
uniform the fast version is used.)
56425643
56435644
*X* and *Y* are 2D arrays with shape (*nr* +1, *nc* +1) that specify
@@ -5651,7 +5652,7 @@ def pcolorfast(self, *args, **kwargs):
56515652
56525653
Note that the column index corresponds to the x-coordinate,
56535654
and the row index corresponds to y; for details, see
5654-
the "Grid Orientation" section below.
5655+
:ref:`Grid Orientation <axes-pcolor-grid-orientation>`.
56555656
56565657
Optional keyword arguments:
56575658

lib/matplotlib/image.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,8 @@ def set_data(self, x, y, A):
839839
"""
840840
Set the grid for the pixel centers, and the pixel values.
841841
842-
*x* and *y* are 1-D ndarrays of lengths N and M, respectively,
843-
specifying pixel centers
842+
*x* and *y* are monotonic 1-D ndarrays of lengths N and M,
843+
respectively, specifying pixel centers
844844
845845
*A* is an (M,N) ndarray or masked array of values to be
846846
colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA
@@ -959,6 +959,19 @@ def _check_unsampled_image(self, renderer):
959959
return False
960960

961961
def set_data(self, x, y, A):
962+
"""
963+
Set the grid for the rectangle boundaries, and the data values.
964+
965+
*x* and *y* are monotonic 1-D ndarrays of lengths N+1 and M+1,
966+
respectively, specifying rectangle boundaries. If None,
967+
they will be created as uniform arrays from 0 through N
968+
and 0 through M, respectively.
969+
970+
*A* is an (M,N) ndarray or masked array of values to be
971+
colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA
972+
array.
973+
974+
"""
962975
A = cbook.safe_masked_invalid(A, copy=True)
963976
if x is None:
964977
x = np.arange(0, A.shape[1]+1, dtype=np.float64)
@@ -985,6 +998,15 @@ def set_data(self, x, y, A):
985998
self.is_grayscale = True
986999
else:
9871000
raise ValueError("3D arrays must have RGB or RGBA as last dim")
1001+
1002+
# For efficient cursor readout, ensure x and y are increasing.
1003+
if x[-1] < x[0]:
1004+
x = x[::-1]
1005+
A = A[:, ::-1]
1006+
if y[-1] < y[0]:
1007+
y = y[::-1]
1008+
A = A[::-1]
1009+
9881010
self._A = A
9891011
self._Ax = x
9901012
self._Ay = y
@@ -994,6 +1016,19 @@ def set_data(self, x, y, A):
9941016
def set_array(self, *args):
9951017
raise NotImplementedError('Method not supported')
9961018

1019+
def get_cursor_data(self, event):
1020+
"""Get the cursor data for a given event"""
1021+
x, y = event.xdata, event.ydata
1022+
if (x < self._Ax[0] or x > self._Ax[-1] or
1023+
y < self._Ay[0] or y > self._Ay[-1]):
1024+
return None
1025+
j = np.searchsorted(self._Ax, x) - 1
1026+
i = np.searchsorted(self._Ay, y) - 1
1027+
try:
1028+
return self._A[i, j]
1029+
except:
1030+
return None
1031+
9971032

9981033
class FigureImage(_ImageBase):
9991034
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)