colors: ensure masked array data is an ndarray #6622

Merged
merged 2 commits into from Jun 22, 2016

Conversation

Projects
None yet
6 participants
Contributor

ngoldbaum commented Jun 21, 2016

This fixes compatibility for imshow plots with array data that is a unit-aware ndarray subclass, for example data from yt.units or astropy.units.

Take for example the following script:

import numpy as np
import matplotlib.pyplot as plt
from yt.units import km

arr = np.random.random((400, 400))*km

plt.imshow(a)
plt.show()

This produces the following traceback right now: https://gist.github.com/ngoldbaum/27effb41173859132fed08a0ad5485ee

This is not isolated to yt's units - if one tries the same thing with astropy's units the same issue arises. Pint does not have this issue because Pint's Quantity class is not an ndarray subclass, so when it is converted to a masked array in matplotlib.image, the units are stripped.

This function seems to expect result.data to be an instance of the base ndarray class. This patch just enforces that expectation explicitly.

@ngoldbaum ngoldbaum colors: ensure masked array data is an ndarray
This fixes compatibility for imshow plots with array data that is a unit-aware
ndarray subclass, for example data frmo yt.units or astropy.units.
1fcf591

mdboom added the needs_review label Jun 21, 2016

Contributor

ngoldbaum commented Jun 21, 2016

Hmm... that failure on travis is worrying. Let me see if I can reproduce it...

Contributor

ngoldbaum commented Jun 21, 2016

Can't reproduce that failure locally...

Owner

jenshnielsen commented Jun 21, 2016

Travis failures are transient and non related

Owner

efiring commented Jun 21, 2016

Looks OK to me, but I suggest adding a brief comment so that someone doesn't look at it a year from now and "optimize" it away, thinking it is superfluous.

@ngoldbaum ngoldbaum colors: add comment explaining use of np.asarray
6c9f8a9
Owner

tacaswell commented Jun 22, 2016

Still not have tagged a v1.5.2, should probably backport this?

Contributor

ngoldbaum commented Jun 22, 2016

If you can backport this for 1.5.2, that would be excellent :)

@tacaswell tacaswell merged commit f8ea2c5 into matplotlib:master Jun 22, 2016

3 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.002%) to 70.265%
Details

tacaswell removed the needs_review label Jun 22, 2016

@tacaswell tacaswell added a commit that referenced this pull request Jun 22, 2016

@tacaswell tacaswell Merge pull request #6622 from data-exp-lab/strip-units-imshow
FIX: ensure masked array data is an ndarray
8d9357e
Owner

tacaswell commented Jun 22, 2016

backported to 1.5.x as 8d9357e

Owner

tacaswell commented Jun 22, 2016

Backported as it fixes a major Yt headache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment