Skip to content

Commit 59ac0e6

Browse files
committed
Let _pcolorargs handle the shape of C, checking compatibility with X and Y
1 parent bf7b501 commit 59ac0e6

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

lib/matplotlib/axes.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7316,13 +7316,15 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
73167316

73177317
return im
73187318

7319-
def _pcolorargs(self, funcname, *args):
7319+
def _pcolorargs(self, funcname, *args, **kw):
7320+
allmatch = kw.pop("allmatch", False)
73207321
if len(args) == 1:
73217322
C = args[0]
73227323
numRows, numCols = C.shape
73237324
X, Y = np.meshgrid(np.arange(numCols + 1), np.arange(numRows + 1))
73247325
elif len(args) == 3:
73257326
X, Y, C = args
7327+
numRows, numCols = C.shape
73267328
else:
73277329
raise TypeError(
73287330
'Illegal arguments to %s; see help(%s)' % (funcname, funcname))
@@ -7339,6 +7341,15 @@ def _pcolorargs(self, funcname, *args):
73397341
raise TypeError(
73407342
'Incompatible X, Y inputs to %s; see help(%s)' % (
73417343
funcname, funcname))
7344+
if allmatch:
7345+
if not (Nx == numCols and Ny == numRows):
7346+
raise TypeError('Dimensions of C are incompatible with'
7347+
' X and/or Y; see help(%s)' % (funcname,))
7348+
else:
7349+
if not (numCols in (Nx, Nx-1) and numRows in (Ny, Ny-1)):
7350+
raise TypeError('Dimensions of C are incompatible with'
7351+
' X and/or Y; see help(%s)' % (funcname,))
7352+
C = C[:Ny-1, :Nx-1]
73427353
return X, Y, C
73437354

73447355
@docstring.dedent_interpd
@@ -7439,7 +7450,7 @@ def pcolor(self, *args, **kwargs):
74397450
74407451
x = np.arange(5)
74417452
y = np.arange(3)
7442-
X, Y = meshgrid(x,y)
7453+
X, Y = np.meshgrid(x, y)
74437454
74447455
is equivalent to::
74457456
@@ -7453,9 +7464,9 @@ def pcolor(self, *args, **kwargs):
74537464
74547465
so if you have::
74557466
7456-
C = rand( len(x), len(y))
7467+
C = rand(len(x), len(y))
74577468
7458-
then you need::
7469+
then you need to transpose C::
74597470
74607471
pcolor(X, Y, C.T)
74617472
@@ -7504,7 +7515,7 @@ def pcolor(self, *args, **kwargs):
75047515
'1.2', 'shading', alternative='edgecolors', obj_type='option')
75057516
shading = kwargs.pop('shading', 'flat')
75067517

7507-
X, Y, C = self._pcolorargs('pcolor', *args)
7518+
X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
75087519
Ny, Nx = X.shape
75097520

75107521
# convert to MA, if necessary.
@@ -7515,7 +7526,7 @@ def pcolor(self, *args, **kwargs):
75157526
xymask = (mask[0:-1, 0:-1] + mask[1:, 1:] +
75167527
mask[0:-1, 1:] + mask[1:, 0:-1])
75177528
# don't plot if C or any of the surrounding vertices are masked.
7518-
mask = ma.getmaskarray(C)[0:Ny - 1, 0:Nx - 1] + xymask
7529+
mask = ma.getmaskarray(C) + xymask
75197530

75207531
newaxis = np.newaxis
75217532
compress = np.compress
@@ -7693,15 +7704,13 @@ def pcolormesh(self, *args, **kwargs):
76937704
antialiased = kwargs.pop('antialiased', False)
76947705
kwargs.setdefault('edgecolors', 'None')
76957706

7696-
X, Y, C = self._pcolorargs('pcolormesh', *args)
7707+
allmatch = (shading == 'gouraud')
7708+
7709+
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
76977710
Ny, Nx = X.shape
76987711

76997712
# convert to one dimensional arrays
7700-
if shading != 'gouraud':
7701-
C = ma.ravel(C[0:Ny - 1, 0:Nx - 1]) # data point in each cell is
7702-
# value at lower left corner
7703-
else:
7704-
C = C.ravel()
7713+
C = C.ravel()
77057714
X = X.ravel()
77067715
Y = Y.ravel()
77077716

0 commit comments

Comments
 (0)