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

Skip to content

Commit 5b40e07

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 50fd321 commit 5b40e07

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
@@ -5731,13 +5731,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
57315731
raise ValueError(
57325732
'x and y arguments to pcolormesh cannot have '
57335733
'non-finite values or be of type '
5734-
'numpy.ma.core.MaskedArray with masked values')
5735-
# safe_masked_invalid() returns an ndarray for dtypes other
5736-
# than floating point.
5737-
if isinstance(X, np.ma.core.MaskedArray):
5738-
X = X.data # strip mask as downstream doesn't like it...
5739-
if isinstance(Y, np.ma.core.MaskedArray):
5740-
Y = Y.data
5734+
'numpy.ma.MaskedArray with masked values')
57415735
nrows, ncols = C.shape[:2]
57425736
else:
57435737
raise _api.nargs_error(funcname, takes="1 or 3", given=len(args))
@@ -5787,10 +5781,8 @@ def _interp_grid(X):
57875781
"This may lead to incorrectly calculated cell "
57885782
"edges, in which case, please supply "
57895783
f"explicit cell edges to {funcname}.")
5790-
if isinstance(X, np.ma.core.MaskedArray):
5791-
hstack = np.ma.hstack
5792-
else:
5793-
hstack = np.hstack
5784+
5785+
hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack
57945786
X = hstack((X[:, [0]] - dX[:, [0]],
57955787
X[:, :-1] + dX,
57965788
X[:, [-1]] + dX[:, [-1]]))
@@ -5983,8 +5975,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
59835975

59845976
kwargs.setdefault('snap', False)
59855977

5986-
if (isinstance(X, np.ma.MaskedArray)
5987-
or isinstance(Y, np.ma.MaskedArray)):
5978+
if np.ma.isMaskedArray(X) or np.ma.isMaskedArray(Y):
59885979
stack = np.ma.stack
59895980
X = np.ma.asarray(X)
59905981
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)