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

Skip to content

BUG: PcolorImage handles non-contiguous arrays, provides data readout #6930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <axes-pcolor-grid-orientation>`)
and the expansion of 1-D *X* and/or *Y* to 2-D arrays.
"""
if not self._hold:
self.cla()
Expand Down Expand Up @@ -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
Expand All @@ -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 <axes-pcolor-grid-orientation>`.

Optional keyword arguments:

Expand Down
39 changes: 37 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -985,6 +998,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]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we enforce that x/y are monotonic someplace else?

If we bump the minimum numpy to 1.7, searchsorted takes in a sorter kwarg which is an array with the indexes in sorted order (https://docs.scipy.org/doc/numpy/reference/generated/numpy.searchsorted.html)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we have never enforced monotonic x or y for any of the pcolor-like functions; that has implicitly been left to the user. If the user supplies non-monotonic x and/or y the plotted results will be odd, and with this PR as-is the cursor readout will sometimes be hard to predict--but the plot won't make sense anyway, so I don't see that it matters. So it seems to me that you are raising a question that is independent of this PR--should we add more input validation for this category of plot?

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
Expand All @@ -994,6 +1016,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
Expand Down
4 changes: 2 additions & 2 deletions src/_image_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down