Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1eb787f

Browse files
committed
MNT: Cleanup PolyQuadMesh
Better handle the subclassing in PolyQuadMesh. Most of the methods should come from the PolyCollection as that is doing the primary drawing. The QuadMesh is really contributing the set_array()/get_array() methods.
1 parent af35462 commit 1eb787f

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5750,13 +5750,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
57505750
raise ValueError(
57515751
'x and y arguments to pcolormesh cannot have '
57525752
'non-finite values or be of type '
5753-
'numpy.ma.core.MaskedArray with masked values')
5754-
# safe_masked_invalid() returns an ndarray for dtypes other
5755-
# than floating point.
5756-
if isinstance(X, np.ma.core.MaskedArray):
5757-
X = X.data # strip mask as downstream doesn't like it...
5758-
if isinstance(Y, np.ma.core.MaskedArray):
5759-
Y = Y.data
5753+
'numpy.ma.MaskedArray with masked values')
57605754
nrows, ncols = C.shape[:2]
57615755
else:
57625756
raise _api.nargs_error(funcname, takes="1 or 3", given=len(args))
@@ -5806,10 +5800,8 @@ def _interp_grid(X):
58065800
"This may lead to incorrectly calculated cell "
58075801
"edges, in which case, please supply "
58085802
f"explicit cell edges to {funcname}.")
5809-
if isinstance(X, np.ma.core.MaskedArray):
5810-
hstack = np.ma.hstack
5811-
else:
5812-
hstack = np.hstack
5803+
5804+
hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack
58135805
X = hstack((X[:, [0]] - dX[:, [0]],
58145806
X[:, :-1] + dX,
58155807
X[:, [-1]] + dX[:, [-1]]))
@@ -6002,8 +5994,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
60025994

60035995
kwargs.setdefault('snap', False)
60045996

6005-
if (isinstance(X, np.ma.MaskedArray)
6006-
or isinstance(Y, np.ma.MaskedArray)):
5997+
if np.ma.isMaskedArray(X) or np.ma.isMaskedArray(Y):
60075998
stack = np.ma.stack
60085999
X = np.ma.asarray(X)
60096000
Y = np.ma.asarray(Y)

lib/matplotlib/collections.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,18 +2170,17 @@ def get_cursor_data(self, event):
21702170

21712171
class PolyQuadMesh(PolyCollection, QuadMesh):
21722172
def __init__(self, coordinates, **kwargs):
2173-
QuadMesh.__init__(self, coordinates=coordinates, **kwargs)
2174-
# Need sizes set because we are using Polycollection.draw()
2175-
self._sizes = np.ones(len(self._coordinates))
2176-
C = self.get_array()
2177-
X = self._coordinates[..., 0]
2178-
Y = self._coordinates[..., 1]
2173+
X = coordinates[..., 0]
2174+
Y = coordinates[..., 1]
21792175

21802176
mask = np.ma.getmaskarray(X) | np.ma.getmaskarray(Y)
2181-
xymask = (mask[0:-1, 0:-1] + mask[1:, 1:] +
2182-
mask[0:-1, 1:] + mask[1:, 0:-1])
2183-
# don't plot if C or any of the surrounding vertices are masked.
2184-
mask = np.ma.getmaskarray(C) | xymask
2177+
# We want the shape of the polygon, which is the corner of
2178+
# each X/Y array
2179+
mask = (mask[0:-1, 0:-1] | mask[1:, 1:] |
2180+
mask[0:-1, 1:] | mask[1:, 0:-1])
2181+
# Take account of the array data too
2182+
C = kwargs.get("array", np.empty(coordinates.shape[:2]))
2183+
mask |= np.ma.getmaskarray(C)
21852184

21862185
unmask = ~mask
21872186
X1 = np.ma.filled(X[:-1, :-1])[unmask]
@@ -2196,9 +2195,5 @@ def __init__(self, coordinates, **kwargs):
21962195

21972196
xy = np.ma.stack([X1, Y1, X2, Y2, X3, Y3, X4, Y4, X1, Y1], axis=-1)
21982197
verts = xy.reshape((npoly, 5, 2))
2199-
self.set_verts(verts)
2200-
2201-
@artist.allow_rasterization
2202-
def draw(self, renderer):
2203-
# We want to use the PolyCollection's draw
2204-
return PolyCollection.draw(self, renderer)
2198+
# We need both verts and coordinates here to go through the super chain
2199+
super().__init__(coordinates=coordinates, verts=verts, **kwargs)

lib/matplotlib/tests/test_collections.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,17 +1068,21 @@ def test_LineCollection_args():
10681068
assert mcolors.same_color(lc.get_facecolor(), 'none')
10691069

10701070

1071-
def test_array_wrong_dimensions():
1071+
@pytest.mark.parametrize("func", (plt.pcolor, plt.pcolormesh))
1072+
def test_array_dimensions(func):
1073+
# Make sure we can set the 1D, 2D, and 3D array shapes
10721074
z = np.arange(12).reshape(3, 4)
1073-
pc = plt.pcolor(z)
1074-
pc.set_array(z)
1075+
pc = func(z)
1076+
# 1D
1077+
pc.set_array(z.ravel())
10751078
pc.update_scalarmappable()
1076-
pc = plt.pcolormesh(z)
1077-
pc.set_array(z) # 2D is OK for Quadmesh
1079+
# 2D
1080+
pc.set_array(z)
10781081
pc.update_scalarmappable()
10791082
# 3D RGB is OK as well
1080-
z = np.arange(36).reshape(3, 4, 3)
1083+
z = np.arange(36, dtype=np.uint8).reshape(3, 4, 3)
10811084
pc.set_array(z)
1085+
pc.update_scalarmappable()
10821086

10831087

10841088
def test_get_segments():

0 commit comments

Comments
 (0)