Skip to content
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
4 changes: 2 additions & 2 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,8 +1516,8 @@ def issubclass_safe(x, klass):
return False


def safe_masked_invalid(x):
x = np.asanyarray(x)
def safe_masked_invalid(x, copy=False):
x = np.array(x, subok=True, copy=copy)
try:
xm = np.ma.masked_invalid(x, copy=False)
xm.shrink_mask()
Expand Down
20 changes: 11 additions & 9 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def set_data(self, A):
if hasattr(A, 'getpixel'):
self._A = pil_to_array(A)
else:
self._A = cbook.safe_masked_invalid(A)
self._A = cbook.safe_masked_invalid(A, copy=True)

if (self._A.dtype != np.uint8 and
not np.can_cast(self._A.dtype, np.float)):
Expand Down Expand Up @@ -798,9 +798,9 @@ def set_data(self, x, y, A):
colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA
array.
"""
x = np.asarray(x, np.float32)
y = np.asarray(y, np.float32)
A = cbook.safe_masked_invalid(A)
x = np.array(x, np.float32)
y = np.array(y, np.float32)
A = cbook.safe_masked_invalid(A, copy=True)
if len(x.shape) != 1 or len(y.shape) != 1\
or A.shape[0:2] != (y.shape[0], x.shape[0]):
raise TypeError("Axes don't match array shape")
Expand Down Expand Up @@ -887,7 +887,8 @@ def __init__(self, ax,
# it needs to be remade if the bbox or viewlim change,
# so caching does help with zoom/pan/resize.
self.update(kwargs)
self.set_data(x, y, A)
if A is not None:
self.set_data(x, y, A)

def make_image(self, magnification=1.0):
if self._A is None:
Expand Down Expand Up @@ -938,15 +939,15 @@ def draw(self, renderer, *args, **kwargs):
self.stale = False

def set_data(self, x, y, A):
A = cbook.safe_masked_invalid(A)
A = cbook.safe_masked_invalid(A, copy=True)
if x is None:
x = np.arange(0, A.shape[1]+1, dtype=np.float64)
else:
x = np.asarray(x, np.float64).ravel()
x = np.array(x, np.float64).ravel()
if y is None:
y = np.arange(0, A.shape[0]+1, dtype=np.float64)
else:
y = np.asarray(y, np.float64).ravel()
y = np.array(y, np.float64).ravel()

if A.shape[:2] != (y.size-1, x.size-1):
print(A.shape)
Expand Down Expand Up @@ -1044,7 +1045,8 @@ def get_extent(self):

def set_data(self, A):
"""Set the image array."""
cm.ScalarMappable.set_array(self, cbook.safe_masked_invalid(A))
cm.ScalarMappable.set_array(self,
cbook.safe_masked_invalid(A, copy=True))
self.stale = True

def set_array(self, A):
Expand Down
47 changes: 46 additions & 1 deletion lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from matplotlib.testing.decorators import (image_comparison,
knownfailureif, cleanup)
from matplotlib.image import BboxImage, imread, NonUniformImage
from matplotlib.image import (BboxImage, imread, NonUniformImage,
AxesImage, FigureImage, PcolorImage)
from matplotlib.transforms import Bbox
from matplotlib import rcParams
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -463,6 +464,50 @@ def test_nonuniformimage_setnorm():
im.set_norm(plt.Normalize())


@cleanup
def test_nonuniformimage_setdata():
ax = plt.gca()
im = NonUniformImage(ax)
x = np.arange(3, dtype=np.float64)
y = np.arange(4, dtype=np.float64)
z = np.arange(12, dtype=np.float64).reshape((4, 3))
im.set_data(x, y, z)
x[0] = y[0] = z[0, 0] = 9.9
assert im._A[0, 0] == im._Ax[0] == im._Ay[0] == 0, 'value changed'


@cleanup
def test_axesimage_setdata():
ax = plt.gca()
im = AxesImage(ax)
z = np.arange(12, dtype=np.float64).reshape((4, 3))
im.set_data(z)
z[0, 0] = 9.9
assert im._A[0, 0] == 0, 'value changed'


@cleanup
def test_figureimage_setdata():
fig = plt.gcf()
im = FigureImage(fig)
z = np.arange(12, dtype=np.float64).reshape((4, 3))
im.set_data(z)
z[0, 0] = 9.9
assert im._A[0, 0] == 0, 'value changed'


@cleanup
def test_pcolorimage_setdata():
ax = plt.gca()
im = PcolorImage(ax)
x = np.arange(3, dtype=np.float64)
y = np.arange(4, dtype=np.float64)
z = np.arange(6, dtype=np.float64).reshape((3, 2))
im.set_data(x, y, z)
x[0] = y[0] = z[0, 0] = 9.9
assert im._A[0, 0] == im._Ax[0] == im._Ay[0] == 0, 'value changed'


@cleanup
def test_minimized_rasterized():
# This ensures that the rasterized content in the colorbars is
Expand Down