Skip to content

Commit 2b8f4d3

Browse files
committed
FIX: handle non-native endian images
In cbook.safe_mask_invalid also ensure that the data is in native byte order. close #6671 closes #6394
1 parent c4fa469 commit 2b8f4d3

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/matplotlib/cbook.py

+7
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,13 @@ def issubclass_safe(x, klass):
15051505

15061506
def safe_masked_invalid(x, copy=False):
15071507
x = np.array(x, subok=True, copy=copy)
1508+
if not x.dtype.isnative:
1509+
# Note that the argument to `byteswap` is 'inplace',
1510+
# thus if we have already made a copy, do the byteswap in
1511+
# place, else make a copy with the byte order swapped.
1512+
# Be explicit that we are swapping the byte order of the dtype
1513+
x = x.byteswap(copy).newbyteorder(new_order='S')
1514+
15081515
try:
15091516
xm = np.ma.masked_invalid(x, copy=False)
15101517
xm.shrink_mask()

lib/matplotlib/tests/test_image.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,21 @@ def test_mask_image():
677677
ax2.imshow(A, interpolation='nearest')
678678

679679

680-
if __name__=='__main__':
681-
import nose
682-
nose.runmodule(argv=['-s','--with-doctest'], exit=False)
680+
@image_comparison(baseline_images=['imshow_endianess'],
681+
remove_text=True, extensions=['png'])
682+
def test_imshow_endianess():
683+
x = np.arange(10)
684+
X, Y = np.meshgrid(x, x)
685+
Z = ((X-5)**2 + (Y-5)**2)**0.5
686+
687+
fig, (ax1, ax2) = plt.subplots(1, 2)
688+
689+
kwargs = dict(origin="lower", interpolation='nearest',
690+
cmap='viridis')
691+
692+
ax1.imshow(Z.astype('<f8'), **kwargs),
693+
ax2.imshow(Z.astype('>f8'), **kwargs),
694+
695+
696+
if __name__ == '__main__':
697+
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)