8000 ENH: allow pcolor to plot all data · matplotlib/matplotlib@01d9209 · GitHub
[go: up one dir, main page]

Skip to content

Commit 01d9209

Browse files
committed
ENH: allow pcolor to plot all data
1 parent e5283e0 commit 01d9209

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5599,7 +5599,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55995599
return im
56005600

56015601
@staticmethod
5602-
def _pcolorargs(funcname, *args, allmatch=False):
5602+
def _pcolorargs(funcname, *args, allmatch=False, dropdata=True):
56035603
# If allmatch is True, then the incoming X, Y, C must have matching
56045604
# dimensions, taking into account that X and Y can be 1-D rather than
56055605
# 2-D. This perfect match is required for Gouraud shading. For flat
@@ -5661,14 +5661,34 @@ def _pcolorargs(funcname, *args, allmatch=False):
56615661
raise TypeError('Dimensions of C %s are incompatible with'
56625662
' X (%d) and/or Y (%d); see help(%s)' % (
56635663
C.shape, Nx, Ny, funcname))
5664-
C = C[:Ny - 1, :Nx - 1]
5664+
5665+
if dropdata:
5666+
C = C[:Ny - 1, :Nx - 1]
5667+
else:
5668+
def _interp_grid(X):
5669+
# helper for below
5670+
dX = np.diff(X, axis=1)/2.
5671+
X = np.hstack((X[:, [0]] - dX[:, [0]],
5672+
X[:, :-1] + dX,
5673+
X[:, [-1]] + dX[:, [-1]])
5674+
)
5675+
return X
5676+
5677+
if ncols == Nx:
5678+
X = _interp_grid(X)
5679+
Y = _interp_grid(Y)
5680+
5681+
if nrows == Ny:
5682+
X = _interp_grid(X.T).T
5683+
Y = _interp_grid(Y.T).T
5684+
56655685
C = cbook.safe_masked_invalid(C)
56665686
return X, Y, C
56675687

56685688
@_preprocess_data()
56695689
@docstring.dedent_interpd
56705690
def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5671-
vmax=None, **kwargs):
5691+
vmax=None, dropdata=True, **kwargs):
56725692
r"""
56735693
Create a pseudocolor plot with a non-regular rectangular grid.
56745694
@@ -5707,7 +5727,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
57075727
57085728
The dimensions of *X* and *Y* should be one greater than those of
57095729
*C*. Alternatively, *X*, *Y* and *C* may have equal dimensions, in
5710-
which case the last row and column of *C* will be ignored.
5730+
which case the last row and column of *C* will be ignored if
5731+
*dropdata* is True (see below).
57115732
57125733
If *X* and/or *Y* are 1-D arrays or column vectors they will be
57135734
expanded as needed into the appropriate 2-D arrays, making a
@@ -5747,6 +5768,12 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
57475768
snap : bool, default: False
57485769
Whether to snap the mesh to pixel boundaries.
57495770
5771+
dropdata: bool, default: True
5772+
If True (default), and X and Y are the same size as C in their
5773+
respective dimensions, drop the last element of C in both
5774+
dimensions. If False, interpolate X and Y to their midpoints
5775+
and extrapolate their endpoints.
5776+
57505777
Returns
57515778
-------
57525779
collection : `matplotlib.collections.Collection`
@@ -5802,7 +5829,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
58025829
Note: This behavior is different from MATLAB's ``pcolor()``, which
58035830
always discards the last row and column of *C*.
58045831
"""
5805-
X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
5832+
X, Y, C = self._pcolorargs('pcolor', *args, dropdata=dropdata,
5833+
allmatch=False)
58065834
Ny, Nx = X.shape
58075835

58085836
# unit conversion allows e.g. datetime objects as axis values
@@ -5899,7 +5927,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
58995927
@_preprocess_data()
59005928
@docstring.dedent_interpd
59015929
def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5902-
vmax=None, shading='flat', antialiased=False, **kwargs):
5930+
vmax=None, shading='flat', antialiased=False,
5931+
dropdata=True, **kwargs):
59035932
"""
59045933
Create a pseudocolor plot with a non-regular rectangular grid.
59055934
@@ -5938,7 +5967,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59385967
59395968
The dimensions of *X* and *Y* should be one greater than those of
59405969
*C*. Alternatively, *X*, *Y* and *C* may have equal dimensions, in
5941-
which case the last row and column of *C* will be ignored.
5970+
which case the last row and column of *C* will be ignored, unless
5971+
*dropdata* is *False* (see below).
59425972
59435973
If *X* and/or *Y* are 1-D arrays or column vectors they will be
59445974
expanded as needed into the appropriate 2-D arrays, making a
@@ -5987,6 +6017,12 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59876017
snap : bool, default: False
59886018
Whether to snap the mesh to pixel boundaries.
59896019
6020+
dropdata: bool, default: True
6021+
If True (default), and X and Y are the same size as C in their
6022+
respective dimensions, drop the last element of C in both
6023+
dimensions. If False, interpolate X and Y to their midpoints
6024+
and extrapolate their endpoints.
6025+
59906026
Returns
59916027
-------
59926028
mesh : `matplotlib.collections.QuadMesh`
@@ -6058,7 +6094,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
60586094

60596095
allmatch = (shading == 'gouraud')
60606096

6061-
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
6097+
X, Y, C = self._pcolorargs('pcolormesh', *args,
6098+
allmatch=allmatch, dropdata=dropdata)
60626099
Ny, Nx = X.shape
60636100
X = X.ravel()
60646101
Y = Y.ravel()

0 commit comments

Comments
 (0)
0