@@ -7316,13 +7316,35 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
7316
7316
7317
7317
return im
7318
7318
7319
- def _pcolorargs (self , funcname , * args ):
7319
+ @staticmethod
7320
+ def _pcolorargs (funcname , * args , ** kw ):
7321
+ # This takes one kwarg, allmatch.
7322
+ # If allmatch is True, then the incoming X, Y, C must
7323
+ # have matching dimensions, taking into account that
7324
+ # X and Y can be 1-D rather than 2-D. This perfect
7325
+ # match is required for Gouroud shading. For flat
7326
+ # shading, X and Y specify boundaries, so we need
7327
+ # one more boundary than color in each direction.
7328
+ # For convenience, and consistent with Matlab, we
7329
+ # discard the last row and/or column of C if necessary
7330
+ # to meet this condition. This is done if allmatch
7331
+ # is False.
7332
+
7333
+ allmatch = kw .pop ("allmatch" , False )
7334
+
7320
7335
if len (args ) == 1 :
7321
7336
C = args [0 ]
7322
7337
numRows , numCols = C .shape
7323
- X , Y = np .meshgrid (np .arange (numCols + 1 ), np .arange (numRows + 1 ))
7324
- elif len (args ) == 3 :
7338
+ if allmatch :
7339
+ X , Y = np .meshgrid (np .arange (numCols ), np .arange (numRows ))
7340
+ else :
7341
+ X , Y = np .meshgrid (np .arange (numCols + 1 ),
7342
+ np .arange (numRows + 1 ))
7343
+ return X , Y , C
7344
+
7345
+ if len (args ) == 3 :
7325
7346
X , Y , C = args
7347
+ numRows , numCols = C .shape
7326
7348
else :
7327
7349
raise TypeError (
7328
7350
'Illegal arguments to %s; see help(%s)' % (funcname , funcname ))
@@ -7339,6 +7361,17 @@ def _pcolorargs(self, funcname, *args):
7339
7361
raise TypeError (
7340
7362
'Incompatible X, Y inputs to %s; see help(%s)' % (
7341
7363
funcname , funcname ))
7364
+ if allmatch :
7365
+ if not (Nx == numCols and Ny == numRows ):
7366
+ raise TypeError ('Dimensions of C %s are incompatible with'
7367
+ ' X (%d) and/or Y (%d); see help(%s)' % (
7368
+ C .shape , Nx , Ny , funcname ))
7369
+ else :
7370
+ if not (numCols in (Nx , Nx - 1 ) and numRows in (Ny , Ny - 1 )):
7371
+ raise TypeError ('Dimensions of C %s are incompatible with'
7372
+ ' X (%d) and/or Y (%d); see help(%s)' % (
7373
+ C .shape , Nx , Ny , funcname ))
7374
+ C = C [:Ny - 1 , :Nx - 1 ]
7342
7375
return X , Y , C
7343
7376
7344
7377
@docstring .dedent_interpd
@@ -7439,7 +7472,7 @@ def pcolor(self, *args, **kwargs):
7439
7472
7440
7473
x = np.arange(5)
7441
7474
y = np.arange(3)
7442
- X, Y = meshgrid(x,y)
7475
+ X, Y = np. meshgrid(x, y)
7443
7476
7444
7477
is equivalent to::
7445
7478
@@ -7453,9 +7486,9 @@ def pcolor(self, *args, **kwargs):
7453
7486
7454
7487
so if you have::
7455
7488
7456
- C = rand( len(x), len(y))
7489
+ C = rand(len(x), len(y))
7457
7490
7458
- then you need::
7491
+ then you need to transpose C ::
7459
7492
7460
7493
pcolor(X, Y, C.T)
7461
7494
@@ -7504,7 +7537,7 @@ def pcolor(self, *args, **kwargs):
7504
7537
'1.2' , 'shading' , alternative = 'edgecolors' , obj_type = 'option' )
7505
7538
shading = kwargs .pop ('shading' , 'flat' )
7506
7539
7507
- X , Y , C = self ._pcolorargs ('pcolor' , * args )
7540
+ X , Y , C = self ._pcolorargs ('pcolor' , * args , allmatch = False )
7508
7541
Ny , Nx = X .shape
7509
7542
7510
7543
# convert to MA, if necessary.
@@ -7515,7 +7548,7 @@ def pcolor(self, *args, **kwargs):
7515
7548
xymask = (mask [0 :- 1 , 0 :- 1 ] + mask [1 :, 1 :] +
7516
7549
mask [0 :- 1 , 1 :] + mask [1 :, 0 :- 1 ])
7517
7550
# 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
7551
+ mask = ma .getmaskarray (C ) + xymask
7519
7552
7520
7553
newaxis = np .newaxis
7521
7554
compress = np .compress
@@ -7693,15 +7726,13 @@ def pcolormesh(self, *args, **kwargs):
7693
7726
antialiased = kwargs .pop ('antialiased' , False )
7694
7727
kwargs .setdefault ('edgecolors' , 'None' )
7695
7728
7696
- X , Y , C = self ._pcolorargs ('pcolormesh' , * args )
7729
+ allmatch = (shading == 'gouraud' )
7730
+
7731
+ X , Y , C = self ._pcolorargs ('pcolormesh' , * args , allmatch = allmatch )
7697
7732
Ny , Nx = X .shape
7698
7733
7699
7734
# 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 ()
7735
+ C = C .ravel ()
7705
7736
X = X .ravel ()
7706
7737
Y = Y .ravel ()
7707
7738
0 commit comments