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

Skip to content

Fix inconsistent behavior in pcolormesh with Gouraud shading #9594

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

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added interpolate_boundaries to pcolormesh to allow for interpolation…
… of X and Y for Gouraud shading in case of dimension mismatch.
  • Loading branch information
simonpf committed Nov 1, 2017
commit dc30842ff37dbe213cd2fd346225ef3d394a8e8d
64 changes: 54 additions & 10 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5178,6 +5178,7 @@ def _pcolorargs(funcname, *args, **kw):
# required for flat shading.

allmatch = kw.pop("allmatch", False)
interpolate_grids = kw.pop("interpolate_grids", False)

if len(args) == 1:
C = np.asanyarray(args[0])
Expand Down Expand Up @@ -5216,15 +5217,25 @@ def _pcolorargs(funcname, *args, **kw):
C.shape, Nx, Ny, funcname))

if allmatch:
if numRows == Ny - 1:
Y_new = 0.5 * Y[:numRows, :numCols]
Y_new += 0.5 * Y[1:, 1:]
Y = Y_new

if numCols == Nx - 1:
X_new = 0.5 * X[:numRows, :numCols]
X_new += 0.5 * X[1:, 1:]
X = X_new
if numRows != Ny or numCols != Nx:
if interpolate_grids:

if numRows == Ny - 1:
Y_new = 0.5 * Y[:numRows, :numCols]
Y_new += 0.5 * Y[1:, 1:]
Y = Y_new

if numCols == Nx - 1:
X_new = 0.5 * X[:numRows, :numCols]
X_new += 0.5 * X[1:, 1:]
X = X_new

else:

raise TypeError('Dimensions of C %s are incompatible with'
' X (%d) and/or Y (%d); see help(%s)' % (
C.shape, Nx, Ny, funcname))

else:
C = C[:Ny - 1, :Nx - 1]
C = cbook.safe_masked_invalid(C)
Expand Down Expand Up @@ -5514,6 +5525,22 @@ def pcolormesh(self, *args, **kwargs):
contrast, :func:`~matplotlib.pyplot.pcolor` simply does not
draw quadrilaterals with masked colors or vertices.

If flat shading is used, the arrays *X* and *Y* are assumed to
specify the boundary points of quadrilaterals. They should thus
each have one more column and row than the *C* array. For compatibility
with Matlab as well as convenience, the case in which *X*, *Y*, *C*
have the same dimension is also accepted and handled by dropping the
last row and column of *C*.

If Gouraud shading is used, the arrays *X* and *Y* are assumed to
specify the centers of the quadrilaterals and thus should have the
same dimensions as *C*. If this is not the case an error will be
thrown. However, for compatibility with the flat shading case, the
``interpolate_grids`` keyword argument is provided. When set to
``True`` and *X* and *Y* have one row and column more then *C*,
*X* and *Y* will be linearly interpolated to obtain the corresponding
centerpoints.

Keyword arguments:

*cmap*: [ *None* | Colormap ]
Expand Down Expand Up @@ -5552,6 +5579,19 @@ def pcolormesh(self, *args, **kwargs):
*alpha*: ``0 <= scalar <= 1`` or *None*
the alpha blending value

*interpolate_grids*: [``True`` | ``False``]

When set to ``True`` and Gouraud shading is used with *X* and
*Y* with one more row and column than *C*, *X* and *Y* will be
linearly interpolated to obtain the centers of the quadrilaterals
described by *X* and *Y*.

When set to ``False`` the above case with cause an error to be
thrown.

Ignored when ``shading = flat``.


Return value is a :class:`matplotlib.collections.QuadMesh`
object.

Expand All @@ -5577,11 +5617,15 @@ def pcolormesh(self, *args, **kwargs):
vmax = kwargs.pop('vmax', None)
shading = kwargs.pop('shading', 'flat').lower()
antialiased = kwargs.pop('antialiased', False)
interpolate_grids = kwargs.pop('interpolate_grids', False)
kwargs.setdefault('edgecolors', 'None')

allmatch = (shading == 'gouraud')

X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
X, Y, C = self._pcolorargs('pcolormesh',
*args,
allmatch=allmatch,
interpolate_grids=interpolate_grids)
Ny, Nx = X.shape
X = X.ravel()
Y = Y.ravel()
Expand Down