Skip to content

Commit e077c13

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 e077c13

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/matplotlib/cbook.py

Lines changed: 7 additions & 0 deletions
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('S')
1514+
15081515
try:
15091516
xm = np.ma.masked_invalid(x, copy=False)
15101517
xm.shrink_mask()

lib/matplotlib/tests/test_image.py

Lines changed: 18 additions & 3 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy