Skip to content

Commit 19d4951

Browse files
committed
DEP: PolyQuadMesh warn when setting with a compressed array
Previously to update a pcolor array you would need to set only the compressed values. This required keeping track of the mask by the user. For consistency with QuadMesh, we should only accept full 2D arrays that include the mask if a user desires. This also allows for the mask to be updated when setting an array.
1 parent 035a6ce commit 19d4951

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/matplotlib/collections.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -2311,9 +2311,21 @@ def get_facecolor(self):
23112311
return fc[unmasked_polys, :]
23122312

23132313
def set_array(self, A):
2314-
prev_mask = self._get_unmasked_polys()
2314+
prev_unmask = self._get_unmasked_polys()
2315+
# MPL <3.8 compressed the mask, so we need to handle flattened 1d input
2316+
# until the deprecation expires, also only warning when there are masked
2317+
# elements and thus compression occurring.
2318+
if (np.ndim(A) == 1 and not np.all(prev_unmask) and
2319+
len(A) == np.sum(prev_unmask)):
2320+
_api.warn_deprecated("3.8", message="Setting a PolyQuadMesh array using "
2321+
"the compressed values is deprecated. "
2322+
"Pass the full 2D shape of the original array "
2323+
f"{prev_unmask.shape} including the masked elements.")
2324+
Afull = np.empty(prev_unmask.shape)
2325+
Afull[prev_unmask] = A
2326+
A = np.ma.array(Afull, mask=~prev_unmask)
23152327
super().set_array(A)
23162328
# If the mask has changed at all we need to update
23172329
# the set of Polys that we are drawing
2318-
if not np.array_equal(prev_mask, self._get_unmasked_polys()):
2330+
if not np.array_equal(prev_unmask, self._get_unmasked_polys()):
23192331
self._set_unmasked_verts()

lib/matplotlib/tests/test_collections.py

+5
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,11 @@ def test_polyquadmesh_masked_vertices_array():
918918
polymesh.update_scalarmappable()
919919
assert len(polymesh.get_paths()) == 4
920920

921+
# Setting array with 1D compressed values is deprecated
922+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
923+
match="Setting a PolyQuadMesh"):
924+
polymesh.set_array(np.ones(4))
925+
921926

922927
def test_quadmesh_get_coordinates(pcfunc):
923928
x = [0, 1, 2]

0 commit comments

Comments
 (0)