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

Skip to content

Commit 5e5ac01

Browse files
committed
Adding 2d support to quadmesh set_array
1 parent cf95c21 commit 5e5ac01

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/matplotlib/collections.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,8 @@ def update_scalarmappable(self):
779779
"""Update colors from the scalar mappable array, if it is not None."""
780780
if self._A is None:
781781
return
782-
if self._A.ndim > 1:
782+
# QuadMesh can map 2d arrays
783+
if self._A.ndim > 1 and not isinstance(self, QuadMesh):
783784
raise ValueError('Collections can only map rank 1 arrays')
784785
if not self._check_update("array"):
785786
return
@@ -2044,8 +2045,10 @@ def draw(self, renderer):
20442045
else:
20452046
renderer.draw_quad_mesh(
20462047
gc, transform.frozen(), self._meshWidth, self._meshHeight,
2047-
coordinates, offsets, transOffset, self.get_facecolor(),
2048-
self._antialiased, self.get_edgecolors())
2048+
coordinates, offsets, transOffset,
2049+
# Backends expect flattened rgba arrays (n*m, 4) for fc and ec
2050+
self.get_facecolor().reshape((-1, 4)),
2051+
self._antialiased, self.get_edgecolors().reshape((-1, 4)))
20492052
gc.restore()
20502053
renderer.close_group(self.__class__.__name__)
20512054
self.stale = False

lib/matplotlib/tests/test_collections.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,20 @@ def test_singleton_autolim():
637637
ax.scatter(0, 0)
638638
np.testing.assert_allclose(ax.get_ylim(), [-0.06, 0.06])
639639
np.testing.assert_allclose(ax.get_xlim(), [-0.06, 0.06])
640+
641+
642+
def test_quadmesh_set_array():
643+
x = np.arange(4)
644+
y = np.arange(4)
645+
z = np.arange(9).reshape((3, 3))
646+
fig, ax = plt.subplots()
647+
coll = ax.pcolormesh(x, y, np.ones(z.shape))
648+
# Test that the collection is able to update with a 2d array
649+
coll.set_array(z)
650+
fig.canvas.draw()
651+
assert np.array_equal(coll.get_array(), z)
652+
653+
# Check that pre-flattened arrays work too
654+
coll.set_array(np.ones(9))
655+
fig.canvas.draw()
656+
assert np.array_equal(coll.get_array(), np.ones(9))

0 commit comments

Comments
 (0)