8000 ENH: allow pcolor to plot all data is x, y not N+1 · matplotlib/matplotlib@1aa6421 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1aa6421

Browse files
committed
ENH: allow pcolor to plot all data is x, y not N+1
1 parent e5283e0 commit 1aa6421

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 57 additions & 13 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]
5 8000 664+
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
@@ -5682,7 +5702,9 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
56825702
56835703
``pcolor()`` can be very slow for large arrays. In most
56845704
cases you should use the similar but much faster
5685-
`~.Axes.pcolormesh` instead. See there for a discussion of the
5705+
`~.Axes.pcolormesh` instead. See
5706+
:ref:`Differences between pcolor() and pcolormesh()
5707+
<differences-pcolor-pcolormesh>` for a discussion of the
56865708
differences.
56875709
56885710
Parameters
@@ -5707,7 +5729,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
57075729
57085730
The dimensions of *X* and *Y* should be one greater than those of
57095731
*C*. Alternatively, *X*, *Y* and *C* may have equal dimensions, in
5710-
which case the last row and column of *C* will be ignored.
5732+
which case the last row and column of *C* will be ignored if
5733+
*dropdata* is True (see below).
57115734
57125735
If *X* and/or *Y* are 1-D arrays or column vectors they will be
57135736
expanded as needed into the appropriate 2-D arrays, making a
@@ -5747,6 +5770,13 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
57475770
snap : bool, default: False
57485771
Whether to snap the mesh to pixel boundaries.
57495772
5773+
dropdata : bool, default: True
5774+
If True (default), and *X* and *Y* are the same size as C in their
5775+
respective dimensions, drop the last element of C in both
5776+
dimensions. If False, *X* and *Y* are assumed to the the midpoints
5777+
of the quadrilaterals, and their edgese are calculated by linear
5778+
interpolation.
5779+
57505780
Returns
57515781
-------
57525782
collection : `matplotlib.collections.Collection`
@@ -5797,12 +5827,16 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
57975827
``pcolor()`` displays all columns of *C* if *X* and *Y* are not
57985828
specified, or if *X* and *Y* have one more column than *C*.
57995829
If *X* and *Y* have the same number of columns as *C* then the last
5800-
column of *C* is dropped. Similarly for the rows.
5830+
column of *C* is dropped if *dropdata* is True. Similarly for the rows.
5831+
If *dropdata* is false, then *X* and *Y* are assumed to be at the
5832+
middle of the quadrilaterals, and the edges of the quadrilaterals are
5833+
linearly interpolated.
58015834
5802-
Note: This behavior is different from MATLAB's ``pcolor()``, which
5835+
This behavior is different from MATLAB's ``pcolor()``, which
58035836
always discards the last row and column of *C*.
58045837
"""
5805-
X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
5838+
X, Y, C = self._pcolorargs('pcolor', *args, dropdata=dropdata,
5839+
allmatch=False)
58065840
Ny, Nx = X.shape
58075841

58085842
# unit conversion allows e.g. datetime objects as axis values
@@ -5899,7 +5933,8 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
58995933
@_preprocess_data()
59005934
@docstring.dedent_interpd
59015935
def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
5902-
vmax=None, shading='flat', antialiased=False, **kwargs):
5936+
vmax=None, shading='flat', antialiased=False,
5937+
dropdata=True, **kwargs):
59035938
"""
59045939
Create a pseudocolor plot with a non-regular rectangular grid.
59055940
@@ -5909,9 +5944,9 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59095944
59105945
*X* and *Y* can be used to specify the corners of the quadrilaterals.
59115946
5912-
.. note::
5947+
.. hint::
59135948
5914-
`~Axes.pcolormesh` is similar to `~Axes.pcolor`. It's much faster
5949+
`~Axes.pcolormesh` is similar to `~Axes.pcolor`. It is much faster
59155950
and preferred in most cases. For a detailed discussion on the
59165951
differences see :ref:`Differences between pcolor() and pcolormesh()
59175952
<differences-pcolor-pcolormesh>`.
@@ -5938,7 +5973,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59385973
59395974
The dimensions of *X* and *Y* should be one greater than those of
59405975
*C*. Alternatively, *X*, *Y* and *C* may have equal dimensions, in
5941-
which case the last row and column of *C* will be ignored.
5976+
which case the last row and column of *C* will be ignored, unless
5977+
*dropdata* is *False* (see below).
59425978
59435979
If *X* and/or *Y* are 1-D arrays or column vectors they will be
59445980
expanded as needed into the appropriate 2-D arrays, making a
@@ -5987,6 +6023,13 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59876023
snap : bool, default: False
59886024
Whether to snap the mesh to pixel boundaries.
59896025
6026+
dropdata : bool, default: True
6027+
If True (default), and *X* and *Y* are the same size as C in their
6028+
respective dimensions, drop the last element of C in both
6029+
dimensions. If False, *X* and *Y* are assumed to the the midpoints
6030+
of the quadrilaterals, and their edgese are calculated by linear
6031+
interpolation.
6032+
59906033
Returns
59916034
-------
59926035
mesh : `matplotlib.collections.QuadMesh`
@@ -6058,7 +6101,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
60586101

60596102
allmatch = (shading == 'gouraud')
60606103

6061-
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
6104+
X, Y, C = self._pcolorargs('pcolormesh', *args,
6105+
allmatch=allmatch, dropdata=dropdata)
60626106
Ny, Nx = X.shape
60636107
X = X.ravel()
60646108
Y = Y.ravel()

lib/matplotlib/pyplot.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,11 +2514,11 @@ def minorticks_on():
25142514
@docstring.copy(Axes.pcolor)
25152515
def pcolor(
25162516
*args, alpha=None, norm=None, cmap=None, vmin=None,
2517-
vmax=None, data=None, **kwargs):
2517+
vmax=None, dropdata=True, data=None, **kwargs):
25182518
__ret = gca().pcolor(
25192519
*args, alpha=alpha, norm=norm, cmap=cmap, vmin=vmin,
2520-
vmax=vmax, **({"data": data} if data is not None else {}),
2521-
**kwargs)
2520+
vmax=vmax, dropdata=dropdata, **({"data": data} if data is not
2521+
None else {}), **kwargs)
25222522
sci(__ret)
25232523
return __ret
25242524

@@ -2527,12 +2527,13 @@ def pcolor(
25272527
@docstring.copy(Axes.pcolormesh)
25282528
def pcolormesh(
25292529
*args, alpha=None, norm=None, cmap=None, vmin=None,
2530-
vmax=None, shading='flat', antialiased=False, data=None,
2531-
**kwargs):
2530+
vmax=None, shading='flat', antialiased=False, dropdata=True,
2531+
data=None, **kwargs):
25322532
__ret = gca().pcolormesh(
25332533
*args, alpha=alpha, norm=norm, cmap=cmap, vmin=vmin,
25342534
vmax=vmax, shading=shading, antialiased=antialiased,
2535-
**({"data": data} if data is not None else {}), **kwargs)
2535+
dropdata=dropdata, **({"data": data} if data is not None else
2536+
{}), **kwargs)
25362537
sci(__ret)
25372538
return __ret
25382539

0 commit comments

Comments
 (0)
0