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

Skip to content

Commit 4554737

Browse files
committed
Quadmesh.set_array validates dimensions
1 parent b6a6414 commit 4554737

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/matplotlib/cm.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,20 @@ def set_array(self, A):
367367
----------
368368
A : ndarray or None
369369
"""
370+
if isinstance(self, mpl.collections.QuadMesh):
371+
A_ = A.ravel()
372+
if self._shading in ['flat', 'nearest', 'auto']:
373+
if not len(A_) == self._meshWidth * self._meshHeight:
374+
raise TypeError(
375+
'Dimensions of A %s are incompatible with '
376+
'X (%d) and/or Y (%d)' %
377+
(A.shape, self._meshWidth, self._meshHeight))
378+
elif self._shading == 'gouraud':
379+
if not len(A_) == (self._meshHeight + 1) * (self._meshWidth + 1):
380+
raise TypeError(
381+
'Dimensions of A %s are incompatible with '
382+
'X (%d) and/or Y (%d)' %
383+
(A.shape, self._meshWidth, self._meshHeight))
370384
self._A = A
371385

372386
def get_array(self):

lib/matplotlib/tests/test_collections.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,35 @@ def test_singleton_autolim():
698698
np.testing.assert_allclose(ax.get_xlim(), [-0.06, 0.06])
699699

700700

701+
def test_quadmesh_set_array_validation():
702+
x = np.arange(4)
703+
y = np.arange(4)
704+
z = np.arange(9).reshape((3, 3))
705+
fig, ax = plt.subplots()
706+
coll = ax.pcolormesh(x, y, np.ones(z.shape))
707+
z = np.arange(16).reshape((4, 4))
708+
709+
with pytest.raises(ValueError, match="Dimensions of A (4, 4) are incompatible with X (3) and/or Y (3)"):
710+
coll.set_array(z)
711+
with pytest.raises(ValueError, match="Dimensions of A (16,) are incompatible with X (3) and/or Y (3)"):
712+
coll.set_array(z.ravel())
713+
714+
fig, ax = plt.subplots()
715+
coll = ax.pcolormesh(x, y, np.ones(z.shape), shading='nearest')
716+
with pytest.raises(ValueError, match="Dimensions of A (3, 3) are incompatible with X (4) and/or Y (4)"):
717+
z = np.arange(9).reshape((3, 3))
718+
coll.set_array(z)
719+
720+
x = np.arange(3)
721+
y = np.arange(3)
722+
z = np.arange(9).reshape((3, 3))
723+
fig, ax = plt.subplots()
724+
coll = ax.pcolormesh(x, y, np.ones(z.shape), shading='gouraud')
725+
z = np.arange(16).reshape((4, 4))
726+
with pytest.raises(ValueError, match="Dimensions of A (4, 4) are incompatible with X (2) and/or Y (2)"):
727+
coll.set_array(z)
728+
729+
701730
def test_quadmesh_set_array():
702731
x = np.arange(4)
703732
y = np.arange(4)

0 commit comments

Comments
 (0)