From 5a4bd7698cae64431e26b8679416a2aac3cc18ae Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sun, 28 Feb 2016 10:24:03 -0500 Subject: [PATCH 1/2] FIX: do invalid masking in to_rgba not set_data The way that color mapping used to work was: - mask invalid - normalize - cmap (which uses the mask information fill in 'bad' values) - interpolate The new order is: - mask invalid - interpolate (converting mask -> nan) - normalize (which passes through nan) - cmap which results in cmap seeing the `nan` which it seems to map to 0. This does the masking on the interpolated array just prior to normalizing and removes it on the way in. Fixes #6069 --- lib/matplotlib/cm.py | 5 ++--- lib/matplotlib/image.py | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index f3a80f470d75..008d24f05349 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -263,9 +263,7 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True): except AttributeError: # e.g., x is not an ndarray; so try mapping it pass - - # This is the normal case, mapping a scalar array: - x = ma.asarray(x) + x = cbook.safe_masked_invalid(x) if norm: x = self.norm(x) rgba = self.cmap(x, alpha=alpha, bytes=bytes) @@ -273,6 +271,7 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True): # transparent so we copy that over to the alpha channel if x.ndim == 2 and x.dtype.kind == 'f': rgba[:, :, 3][x < 0.0] = 0 + return rgba def set_array(self, A): diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index ade8bd8a28fe..2d10427268c4 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -503,9 +503,9 @@ def set_data(self, A): """ # check if data is PIL Image without importing Image if hasattr(A, 'getpixel'): - self._A = pil_to_array(A) - else: - self._A = cbook.safe_masked_invalid(A) + A = pil_to_array(A) + + self._A = A if (self._A.dtype != np.uint8 and not np.can_cast(self._A.dtype, np.float)): From 4bd93128ff6e1e196bed54fd905f6b7bacb9cf6b Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 29 Feb 2016 15:41:12 -0500 Subject: [PATCH 2/2] FIX: always cast to an array on way in --- lib/matplotlib/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 2d10427268c4..350668f0f538 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -505,7 +505,7 @@ def set_data(self, A): if hasattr(A, 'getpixel'): A = pil_to_array(A) - self._A = A + self._A = np.asanyarray(A) if (self._A.dtype != np.uint8 and not np.can_cast(self._A.dtype, np.float)):