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

Skip to content

Commit 923e880

Browse files
author
simonpf
committed
Added interpolate_boundaries to pcolormesh to allow for interpolation of X and Y for Gouraud shading in case of dimension mismatch.
1 parent f6c98a5 commit 923e880

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5238,6 +5238,7 @@ def _pcolorargs(funcname, *args, **kw):
52385238
# required for flat shading.
52395239

52405240
allmatch = kw.pop("allmatch", False)
5241+
interpolate_grids = kw.pop("interpolate_grids", False)
52415242

52425243
if len(args) == 1:
52435244
C = np.asanyarray(args[0])
@@ -5276,15 +5277,25 @@ def _pcolorargs(funcname, *args, **kw):
52765277
C.shape, Nx, Ny, funcname))
52775278

52785279
if allmatch:
5279-
if numRows == Ny - 1:
5280-
Y_new = 0.5 * Y[:numRows, :numCols]
5281-
Y_new += 0.5 * Y[1:, 1:]
5282-
Y = Y_new
5283-
5284-
if numCols == Nx - 1:
5285-
X_new = 0.5 * X[:numRows, :numCols]
5286-
X_new += 0.5 * X[1:, 1:]
5287-
X = X_new
5280+
if numRows != Ny or numCols != Nx:
5281+
if interpolate_grids:
5282+
5283+
if numRows == Ny - 1:
5284+
Y_new = 0.5 * Y[:numRows, :numCols]
5285+
Y_new += 0.5 * Y[1:, 1:]
5286+
Y = Y_new
5287+
5288+
if numCols == Nx - 1:
5289+
X_new = 0.5 * X[:numRows, :numCols]
5290+
X_new += 0.5 * X[1:, 1:]
5291+
X = X_new
5292+
5293+
else:
5294+
5295+
raise TypeError('Dimensions of C %s are incompatible with'
5296+
' X (%d) and/or Y (%d); see help(%s)' % (
5297+
C.shape, Nx, Ny, funcname))
5298+
52885299
else:
52895300
C = C[:Ny - 1, :Nx - 1]
52905301
C = cbook.safe_masked_invalid(C)
@@ -5574,6 +5585,22 @@ def pcolormesh(self, *args, **kwargs):
55745585
contrast, :func:`~matplotlib.pyplot.pcolor` simply does not
55755586
draw quadrilaterals with masked colors or vertices.
55765587
5588+
If flat shading is used, the arrays *X* and *Y* are assumed to
5589+
specify the boundary points of quadrilaterals with colors given
5590+
in *C*. They should thus each have one more column and row than
5591+
the *C* array. For compatibility with Matlab as well as convenience,
5592+
the case in which *X*, *Y*, *C* have the same dimension
5593+
is also accepted and handled by dropping the last row and column
5594+
of *C*.
5595+
5596+
If Gouraud shading is used, the arrays *X* and *Y* are assumed to
5597+
specify the centers of the quadrilaterals and thus should have the
5598+
same dimensions as *C*. If this is not the case and error will be
5599+
thrown. However, for compatibility with the flat shading case, the
5600+
``interpolate_grids`` keyword argument is provided which when
5601+
set to ``True`` will cause *X* and *Y* to be linearly interpolated
5602+
to obtain the corresponding centerpoints.
5603+
55775604
Keyword arguments:
55785605
55795606
*cmap*: [ *None* | Colormap ]
@@ -5612,6 +5639,19 @@ def pcolormesh(self, *args, **kwargs):
56125639
*alpha*: ``0 <= scalar <= 1`` or *None*
56135640
the alpha blending value
56145641
5642+
*interpolate_grids*: [``True`` | ``False``]
5643+
5644+
When set to ``True`` and Gouraud shading is used with *X* and
5645+
*Y* with one more row and column than *C*, *X* and *Y* will be
5646+
linearly interpolated to obtain the centers of the quadrilaterals
5647+
described by *X* and *Y*.
5648+
5649+
When set to ``False`` the above case with cause an error to be
5650+
thrown.
5651+
5652+
Ignored when ``shading = flat``.
5653+
5654+
56155655
Return value is a :class:`matplotlib.collections.QuadMesh`
56165656
object.
56175657
@@ -5637,11 +5677,15 @@ def pcolormesh(self, *args, **kwargs):
56375677
vmax = kwargs.pop('vmax', None)
56385678
shading = kwargs.pop('shading', 'flat').lower()
56395679
antialiased = kwargs.pop('antialiased', False)
5680+
interpolate_grids = kwargs.pop('interpolate_grids', False)
56405681
kwargs.setdefault('edgecolors', 'None')
56415682

56425683
allmatch = (shading == 'gouraud')
56435684

5644-
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
5685+
X, Y, C = self._pcolorargs('pcolormesh',
5686+
*args,
5687+
allmatch=allmatch,
5688+
interpolate_grids=interpolate_grids)
56455689
Ny, Nx = X.shape
56465690
X = X.ravel()
56475691
Y = Y.ravel()

0 commit comments

Comments
 (0)