From ebfe81dda5c8bb1c2c2a751a2c8d8147c849bea9 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Mon, 8 Aug 2016 16:19:07 -1000 Subject: [PATCH 1/2] BUG: PcolorImage handles non-contiguous arrays, provides data readout --- lib/matplotlib/image.py | 22 ++++++++++++++++++++++ src/_image_wrapper.cpp | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 59209fc7755c..95f61a456de1 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -985,6 +985,15 @@ def set_data(self, x, y, A): self.is_grayscale = True else: raise ValueError("3D arrays must have RGB or RGBA as last dim") + + # For efficient cursor readout, ensure x and y are increasing. + if x[-1] < x[0]: + x = x[::-1] + A = A[:, ::-1] + if y[-1] < y[0]: + y = y[::-1] + A = A[::-1] + self._A = A self._Ax = x self._Ay = y @@ -994,6 +1003,19 @@ def set_data(self, x, y, A): def set_array(self, *args): raise NotImplementedError('Method not supported') + def get_cursor_data(self, event): + """Get the cursor data for a given event""" + x, y = event.xdata, event.ydata + if (x < self._Ax[0] or x > self._Ax[-1] or + y < self._Ay[0] or y > self._Ay[-1]): + return None + j = np.searchsorted(self._Ax, x) - 1 + i = np.searchsorted(self._Ay, y) - 1 + try: + return self._A[i, j] + except: + return None + class FigureImage(_ImageBase): zorder = 0 diff --git a/src/_image_wrapper.cpp b/src/_image_wrapper.cpp index aac86bf93673..ed7cb50e4a10 100644 --- a/src/_image_wrapper.cpp +++ b/src/_image_wrapper.cpp @@ -411,9 +411,9 @@ static PyObject *image_pcolor2(PyObject *self, PyObject *args, PyObject *kwds) if (!PyArg_ParseTuple(args, "O&O&O&II(ffff)O&:pcolor2", - &x.converter, + &x.converter_contiguous, &x, - &y.converter, + &y.converter_contiguous, &y, &d.converter_contiguous, &d, From f1b6135b12b6c4049d9a82025c04539458ea4238 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Wed, 24 Aug 2016 07:49:41 -1000 Subject: [PATCH 2/2] DOC: add docstring for set_array; improve related docstrings in _axes.py --- lib/matplotlib/axes/_axes.py | 15 ++++++++------- lib/matplotlib/image.py | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index bd3897cc3a44..49af46e99ba1 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5522,8 +5522,9 @@ def pcolormesh(self, *args, **kwargs): .. seealso:: :func:`~matplotlib.pyplot.pcolor` - For an explanation of the grid orientation and the - expansion of 1-D *X* and/or *Y* to 2-D arrays. + For an explanation of the grid orientation + (:ref:`Grid Orientation `) + and the expansion of 1-D *X* and/or *Y* to 2-D arrays. """ if not self._hold: self.cla() @@ -5637,10 +5638,10 @@ def pcolorfast(self, *args, **kwargs): (*nr*-1, *nc*-1). All cells are rectangles of the same size. This is the fastest version. - *x*, *y* are 1D arrays of length *nc* +1 and *nr* +1, respectively, - giving the x and y boundaries of the cells. Hence the cells are - rectangular but the grid may be nonuniform. The speed is - intermediate. (The grid is checked, and if found to be + *x*, *y* are monotonic 1D arrays of length *nc* +1 and *nr* +1, + respectively, giving the x and y boundaries of the cells. Hence + the cells are rectangular but the grid may be nonuniform. The + speed is intermediate. (The grid is checked, and if found to be uniform the fast version is used.) *X* and *Y* are 2D arrays with shape (*nr* +1, *nc* +1) that specify @@ -5654,7 +5655,7 @@ def pcolorfast(self, *args, **kwargs): Note that the column index corresponds to the x-coordinate, and the row index corresponds to y; for details, see - the "Grid Orientation" section below. + :ref:`Grid Orientation `. Optional keyword arguments: diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 95f61a456de1..9e1646f9bc94 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -839,8 +839,8 @@ def set_data(self, x, y, A): """ Set the grid for the pixel centers, and the pixel values. - *x* and *y* are 1-D ndarrays of lengths N and M, respectively, - specifying pixel centers + *x* and *y* are monotonic 1-D ndarrays of lengths N and M, + respectively, specifying pixel centers *A* is an (M,N) ndarray or masked array of values to be colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA @@ -959,6 +959,19 @@ def _check_unsampled_image(self, renderer): return False def set_data(self, x, y, A): + """ + Set the grid for the rectangle boundaries, and the data values. + + *x* and *y* are monotonic 1-D ndarrays of lengths N+1 and M+1, + respectively, specifying rectangle boundaries. If None, + they will be created as uniform arrays from 0 through N + and 0 through M, respectively. + + *A* is an (M,N) ndarray or masked array of values to be + colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA + array. + + """ A = cbook.safe_masked_invalid(A, copy=True) if x is None: x = np.arange(0, A.shape[1]+1, dtype=np.float64)