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

Skip to content

Adding 2d support to quadmesh set_array #16908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,8 @@ def update_scalarmappable(self):
"""Update colors from the scalar mappable array, if it is not None."""
if self._A is None:
return
if self._A.ndim > 1:
# QuadMesh can map 2d arrays
if self._A.ndim > 1 and not isinstance(self, QuadMesh):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do the raveling here as

if isinstance(self, QuadMesh):
    self._A = self._A.ravel()
else:
    raise ValueError(...)

It keeps everything a bit more consistent shape wise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually what I had originally, but force-pushed over. The downside to it is if someone calls get_array() it is a different shape returned, which could cause confusion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a fair point, but I am also worried about the shape stability of people who have code written against QuadMesh who are now going to be suprised that sometimes they get back 2d data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should only happen if they pass in 2d data, which was not possible before. So, I think all the before cases were 1d inputs and will return 1d inputs still. This is really for my selfish future motivation of wanting to call update animations without forgetting to ravel() and get the ValueError thrown my way. I completely agree though, it does add another layer of potential confusion and that should be weighed on pros/cons.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried about the (hypothetical) that someone has written a function that takes in a QuadMesh, uses get_array(), and assumes 1d data. If we do the reshaping at the last minute then that assumption is no longer valid, but there is no way for the function author to reasonably know.

I do see both sides of this and neither is obviously better.

raise ValueError('Collections can only map rank 1 arrays')
if not self._check_update("array"):
return
Expand Down Expand Up @@ -2044,8 +2045,10 @@ def draw(self, renderer):
else:
renderer.draw_quad_mesh(
gc, transform.frozen(), self._meshWidth, self._meshHeight,
coordinates, offsets, transOffset, self.get_facecolor(),
self._antialiased, self.get_edgecolors())
coordinates, offsets, transOffset,
# Backends expect flattened rgba arrays (n*m, 4) for fc and ec
self.get_facecolor().reshape((-1, 4)),
self._antialiased, self.get_edgecolors().reshape((-1, 4)))
gc.restore()
renderer.close_group(self.__class__.__name__)
self.stale = False
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,20 @@ def test_singleton_autolim():
ax.scatter(0, 0)
np.testing.assert_allclose(ax.get_ylim(), [-0.06, 0.06])
np.testing.assert_allclose(ax.get_xlim(), [-0.06, 0.06])


def test_quadmesh_set_array():
x = np.arange(4)
y = np.arange(4)
z = np.arange(9).reshape((3, 3))
fig, ax = plt.subplots()
coll = ax.pcolormesh(x, y, np.ones(z.shape))
# Test that the collection is able to update with a 2d array
coll.set_array(z)
fig.canvas.draw()
assert np.array_equal(coll.get_array(), z)

# Check that pre-flattened arrays work too
coll.set_array(np.ones(9))
fig.canvas.draw()
assert np.array_equal(coll.get_array(), np.ones(9))