@@ -7316,13 +7316,35 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
73167316
73177317 return im
73187318
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+
73207335 if len (args ) == 1 :
73217336 C = args [0 ]
73227337 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 :
73257346 X , Y , C = args
7347+ numRows , numCols = C .shape
73267348 else :
73277349 raise TypeError (
73287350 'Illegal arguments to %s; see help(%s)' % (funcname , funcname ))
@@ -7339,6 +7361,17 @@ def _pcolorargs(self, funcname, *args):
73397361 raise TypeError (
73407362 'Incompatible X, Y inputs to %s; see help(%s)' % (
73417363 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 ]
73427375 return X , Y , C
73437376
73447377 @docstring .dedent_interpd
@@ -7439,7 +7472,7 @@ def pcolor(self, *args, **kwargs):
74397472
74407473 x = np.arange(5)
74417474 y = np.arange(3)
7442- X, Y = meshgrid(x,y)
7475+ X, Y = np. meshgrid(x, y)
74437476
74447477 is equivalent to::
74457478
@@ -7453,9 +7486,9 @@ def pcolor(self, *args, **kwargs):
74537486
74547487 so if you have::
74557488
7456- C = rand( len(x), len(y))
7489+ C = rand(len(x), len(y))
74577490
7458- then you need::
7491+ then you need to transpose C ::
74597492
74607493 pcolor(X, Y, C.T)
74617494
@@ -7504,7 +7537,7 @@ def pcolor(self, *args, **kwargs):
75047537 '1.2' , 'shading' , alternative = 'edgecolors' , obj_type = 'option' )
75057538 shading = kwargs .pop ('shading' , 'flat' )
75067539
7507- X , Y , C = self ._pcolorargs ('pcolor' , * args )
7540+ X , Y , C = self ._pcolorargs ('pcolor' , * args , allmatch = False )
75087541 Ny , Nx = X .shape
75097542
75107543 # convert to MA, if necessary.
@@ -7515,7 +7548,7 @@ def pcolor(self, *args, **kwargs):
75157548 xymask = (mask [0 :- 1 , 0 :- 1 ] + mask [1 :, 1 :] +
75167549 mask [0 :- 1 , 1 :] + mask [1 :, 0 :- 1 ])
75177550 # 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
75197552
75207553 newaxis = np .newaxis
75217554 compress = np .compress
@@ -7693,15 +7726,13 @@ def pcolormesh(self, *args, **kwargs):
76937726 antialiased = kwargs .pop ('antialiased' , False )
76947727 kwargs .setdefault ('edgecolors' , 'None' )
76957728
7696- X , Y , C = self ._pcolorargs ('pcolormesh' , * args )
7729+ allmatch = (shading == 'gouraud' )
7730+
7731+ X , Y , C = self ._pcolorargs ('pcolormesh' , * args , allmatch = allmatch )
76977732 Ny , Nx = X .shape
76987733
76997734 # 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 ()
77057736 X = X .ravel ()
77067737 Y = Y .ravel ()
77077738
0 commit comments