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

Skip to content

Commit a6615e1

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 92e4c70 commit a6615e1

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
@@ -5717,13 +5717,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
57175717
raise ValueError(
57185718
'x and y arguments to pcolormesh cannot have '
57195719
'non-finite values or be of type '
5720-
'numpy.ma.core.MaskedArray with masked values')
5721-
# safe_masked_invalid() returns an ndarray for dtypes other
5722-
# than floating point.
5723-
if isinstance(X, np.ma.core.MaskedArray):
5724-
X = X.data # strip mask as downstream doesn't like it...
5725-
if isinstance(Y, np.ma.core.MaskedArray):
5726-
Y = Y.data
5720+
'numpy.ma.MaskedArray with masked values')
57275721
nrows, ncols = C.shape[:2]
57285722
else:
57295723
raise _api.nargs_error(funcname, takes="1 or 3", given=len(args))
@@ -5773,10 +5767,8 @@ def _interp_grid(X):
57735767
"This may lead to incorrectly calculated cell "
57745768
"edges, in which case, please supply "
57755769
f"explicit cell edges to {funcname}.")
5776-
if isinstance(X, np.ma.core.MaskedArray):
5777-
hstack = np.ma.hstack
5778-
else:
5779-
hstack = np.hstack
5770+
5771+
hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack
57805772
X = hstack((X[:, [0]] - dX[:, [0]],
57815773
X[:, :-1] + dX,
57825774
X[:, [-1]] + dX[:, [-1]]))
@@ -5969,8 +5961,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
59695961

59705962
kwargs.setdefault('snap', False)
59715963

5972-
if (isinstance(X, np.ma.MaskedArray)
5973-
or isinstance(Y, np.ma.MaskedArray)):
5964+
if np.ma.isMaskedArray(X) or np.ma.isMaskedArray(Y):
59745965
stack = np.ma.stack
59755966
X = np.ma.asarray(X)
59765967
Y = np.ma.asarray(Y)

lib/matplotlib/collections.py

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

21162116
class PolyQuadMesh(PolyCollection, QuadMesh):
21172117
def __init__(self, coordinates, **kwargs):
2118-
QuadMesh.__init__(self, coordinates=coordinates, **kwargs)
2119-
# Need sizes set because we are using Polycollection.draw()
2120-
self._sizes = np.ones(len(self._coordinates))
2121-
C = self.get_array()
2122-
X = self._coordinates[..., 0]
2123-
Y = self._coordinates[..., 1]
2118+
X = coordinates[..., 0]
2119+
Y = coordinates[..., 1]
21242120

21252121
mask = np.ma.getmaskarray(X) | np.ma.getmaskarray(Y)
2126-
xymask = (mask[0:-1, 0:-1] + mask[1:, 1:] +
2127-
mask[0:-1, 1:] + mask[1:, 0:-1])
2128-
# don't plot if C or any of the surrounding vertices are masked.
2129-
mask = np.ma.getmaskarray(C) | xymask
2122+
# We want the shape of the polygon, which is the corner of
2123+
# each X/Y array
2124+
mask = (mask[0:-1, 0:-1] | mask[1:, 1:] |
2125+
mask[0:-1, 1:] | mask[1:, 0:-1])
2126+
# Take account of the array data too
2127+
C = kwargs.get("array", np.empty(coordinates.shape[:2]))
2128+
mask |= np.ma.getmaskarray(C)
21302129

21312130
unmask = ~mask
21322131
X1 = np.ma.filled(X[:-1, :-1])[unmask]
@@ -2141,9 +2140,5 @@ def __init__(self, coordinates, **kwargs):
21412140

21422141
xy = np.ma.stack([X1, Y1, X2, Y2, X3, Y3, X4, Y4, X1, Y1], axis=-1)
21432142
verts = xy.reshape((npoly, 5, 2))
2144-
self.set_verts(verts)
2145-
2146-
@artist.allow_rasterization
2147-
def draw(self, renderer):
2148-
# We want to use the PolyCollection's draw
2149-
return PolyCollection.draw(self, renderer)
2143+
# We need both verts and coordinates here to go through the super chain
2144+
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
@@ -1067,17 +1067,21 @@ def test_LineCollection_args():
10671067
assert mcolors.same_color(lc.get_facecolor(), 'none')
10681068

10691069

1070-
def test_array_wrong_dimensions():
1070+
@pytest.mark.parametrize("func", (plt.pcolor, plt.pcolormesh))
1071+
def test_array_dimensions(func):
1072+
# Make sure we can set the 1D, 2D, and 3D array shapes
10711073
z = np.arange(12).reshape(3, 4)
1072-
pc = plt.pcolor(z)
1073-
pc.set_array(z)
1074+
pc = func(z)
1075+
# 1D
1076+
pc.set_array(z.ravel())
10741077
pc.update_scalarmappable()
1075-
pc = plt.pcolormesh(z)
1076-
pc.set_array(z) # 2D is OK for Quadmesh
1078+
# 2D
1079+
pc.set_array(z)
10771080
pc.update_scalarmappable()
10781081
# 3D RGB is OK as well
1079-
z = np.arange(36).reshape(3, 4, 3)
1082+
z = np.arange(36, dtype=np.uint8).reshape(3, 4, 3)
10801083
pc.set_array(z)
1084+
pc.update_scalarmappable()
10811085

10821086

10831087
def test_get_segments():

0 commit comments

Comments
 (0)