@@ -2242,24 +2242,38 @@ class PolyQuadMesh(_MeshData, PolyCollection):
22422242
22432243 def __init__ (self , coordinates , ** kwargs ):
22442244 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 ]
22602275
2261- unmask = ~ mask
2262- self ._valid_polys = unmask .ravel ()
2276+ unmask = self ._get_unmasked_polys ()
22632277 X1 = np .ma .filled (X [:- 1 , :- 1 ])[unmask ]
22642278 Y1 = np .ma .filled (Y [:- 1 , :- 1 ])[unmask ]
22652279 X2 = np .ma .filled (X [1 :, :- 1 ])[unmask ]
@@ -2272,25 +2286,34 @@ def __init__(self, coordinates, **kwargs):
22722286
22732287 xy = np .ma .stack ([X1 , Y1 , X2 , Y2 , X3 , Y3 , X4 , Y4 , X1 , Y1 ], axis = - 1 )
22742288 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 )
22772290
22782291 def get_edgecolor (self ):
22792292 # docstring inherited
22802293 # We only want to return the facecolors of the polygons
22812294 # that were drawn.
22822295 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 ):
22842298 # Mapping is off
22852299 return ec
2286- return ec [self . _valid_polys , :]
2300+ return ec [unmasked_polys , :]
22872301
22882302 def get_facecolor (self ):
22892303 # docstring inherited
22902304 # We only want to return the facecolors of the polygons
22912305 # that were drawn.
22922306 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 ):
22942309 # Mapping is off
22952310 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