@@ -2242,24 +2242,38 @@ class PolyQuadMesh(_MeshData, PolyCollection):
2242
2242
2243
2243
def __init__ (self , coordinates , ** kwargs ):
2244
2244
super ().__init__ (coordinates = coordinates )
2245
- X = coordinates [..., 0 ]
2246
- Y = coordinates [..., 1 ]
2247
-
2248
- mask = np .ma .getmaskarray (X ) | np .ma .getmaskarray (Y )
2249
- # We want the shape of the polygon, which is the corner of
2250
- # each X/Y array
2251
- mask = (mask [0 :- 1 , 0 :- 1 ] | mask [1 :, 1 :] |
2252
- mask [0 :- 1 , 1 :] | mask [1 :, 0 :- 1 ])
2253
- # Take account of the array data too
2254
- C = kwargs .get ("array" , np .empty (coordinates .shape [:2 ]))
2255
- if C .ndim == 3 :
2256
- # RGB(A) case
2257
- mask |= np .any (np .ma .getmaskarray (C ), axis = - 1 )
2258
- else :
2259
- mask |= np .ma .getmaskarray (C )
2245
+ PolyCollection .__init__ (self , verts = [], ** kwargs )
2246
+ # Setting the verts updates the paths of the PolyCollection
2247
+ # This is called after the initializers to make sure the kwargs
2248
+ # have all been processed and available for the masking calculations
2249
+ self ._set_unmasked_verts ()
2250
+
2251
+ def _get_unmasked_polys (self ):
2252
+ """Get the unmasked regions using the coordinates and array"""
2253
+ # mask(X) | mask(Y)
2254
+ mask = np .any (np .ma .getmaskarray (self ._coordinates ), axis = - 1 )
2255
+
2256
+ # We want the shape of the polygon, which is the corner of each X/Y array
2257
+ mask = (mask [0 :- 1 , 0 :- 1 ] | mask [1 :, 1 :] | mask [0 :- 1 , 1 :] | mask [1 :, 0 :- 1 ])
2258
+
2259
+ # Take account of the array data too, expand if it is None
2260
+ arr = self .get_array ()
2261
+ if arr is not None :
2262
+ arr = np .ma .getmaskarray (arr )
2263
+ if arr .ndim == 3 :
2264
+ # RGB(A) case
2265
+ mask |= np .any (arr , axis = - 1 )
2266
+ elif arr .ndim == 2 :
2267
+ mask |= arr
2268
+ else :
2269
+ mask |= arr .reshape (self ._coordinates [:- 1 , :- 1 , :].shape [:2 ])
2270
+ return ~ mask
2271
+
2272
+ def _set_unmasked_verts (self ):
2273
+ X = self ._coordinates [..., 0 ]
2274
+ Y = self ._coordinates [..., 1 ]
2260
2275
2261
- unmask = ~ mask
2262
- self ._valid_polys = unmask .ravel ()
2276
+ unmask = self ._get_unmasked_polys ()
2263
2277
X1 = np .ma .filled (X [:- 1 , :- 1 ])[unmask ]
2264
2278
Y1 = np .ma .filled (Y [:- 1 , :- 1 ])[unmask ]
2265
2279
X2 = np .ma .filled (X [1 :, :- 1 ])[unmask ]
@@ -2272,25 +2286,34 @@ def __init__(self, coordinates, **kwargs):
2272
2286
2273
2287
xy = np .ma .stack ([X1 , Y1 , X2 , Y2 , X3 , Y3 , X4 , Y4 , X1 , Y1 ], axis = - 1 )
2274
2288
verts = xy .reshape ((npoly , 5 , 2 ))
2275
- # Setting the verts updates the paths of the PolyCollection
2276
- PolyCollection .__init__ (self , verts = verts , ** kwargs )
2289
+ self .set_verts (verts )
2277
2290
2278
2291
def get_edgecolor (self ):
2279
2292
# docstring inherited
2280
2293
# We only want to return the facecolors of the polygons
2281
2294
# that were drawn.
2282
2295
ec = super ().get_edgecolor ()
2283
- if len (ec ) != len (self ._valid_polys ):
2296
+ unmasked_polys = self ._get_unmasked_polys ().ravel ()
2297
+ if len (ec ) != len (unmasked_polys ):
2284
2298
# Mapping is off
2285
2299
return ec
2286
- return ec [self . _valid_polys , :]
2300
+ return ec [unmasked_polys , :]
2287
2301
2288
2302
def get_facecolor (self ):
2289
2303
# docstring inherited
2290
2304
# We only want to return the facecolors of the polygons
2291
2305
# that were drawn.
2292
2306
fc = super ().get_facecolor ()
2293
- if len (fc ) != len (self ._valid_polys ):
2307
+ unmasked_polys = self ._get_unmasked_polys ().ravel ()
2308
+ if len (fc ) != len (unmasked_polys ):
2294
2309
# Mapping is off
2295
2310
return fc
2296
- return fc [self ._valid_polys , :]
2311
+ return fc [unmasked_polys , :]
2312
+
2313
+ def set_array (self , A ):
2314
+ prev_mask = self ._get_unmasked_polys ()
2315
+ super ().set_array (A )
2316
+ # If the mask has changed at all we need to update
2317
+ # the set of Polys that we are drawing
2318
+ if not np .array_equal (prev_mask , self ._get_unmasked_polys ()):
2319
+ self ._set_unmasked_verts ()
0 commit comments