Skip to content

Commit

Permalink
FIX: handle non-native endian images
Browse files Browse the repository at this point in the history
In cbook.safe_mask_invalid also ensure that the data is in
native byte order.

close matplotlib#6671 closes matplotlib#6394
  • Loading branch information
tacaswell committed Jul 2, 2016
1 parent c4fa469 commit 2b8f4d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/matplotlib/cbook.py
Expand Up @@ -1505,6 +1505,13 @@ def issubclass_safe(x, klass):

def safe_masked_invalid(x, copy=False):
x = np.array(x, subok=True, copy=copy)
if not x.dtype.isnative:
# Note that the argument to `byteswap` is 'inplace',
# thus if we have already made a copy, do the byteswap in
# place, else make a copy with the byte order swapped.
# Be explicit that we are swapping the byte order of the dtype
x = x.byteswap(copy).newbyteorder(new_order='S')

try:
xm = np.ma.masked_invalid(x, copy=False)
xm.shrink_mask()
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 18 additions & 3 deletions lib/matplotlib/tests/test_image.py
Expand Up @@ -677,6 +677,21 @@ def test_mask_image():
ax2.imshow(A, interpolation='nearest')


if __name__=='__main__':
import nose
nose.runmodule(argv=['-s','--with-doctest'], exit=False)
@image_comparison(baseline_images=['imshow_endianess'],
remove_text=True, extensions=['png'])
def test_imshow_endianess():
x = np.arange(10)
X, Y = np.meshgrid(x, x)
Z = ((X-5)**2 + (Y-5)**2)**0.5

fig, (ax1, ax2) = plt.subplots(1, 2)

kwargs = dict(origin="lower", interpolation='nearest',
cmap='viridis')

ax1.imshow(Z.astype('<f8'), **kwargs),
ax2.imshow(Z.astype('>f8'), **kwargs),


if __name__ == '__main__':
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 comments on commit 2b8f4d3

Please sign in to comment.