imshow seems to fail on some (int16?) arrays

Jon Olav Vik <jonovik@...83...> writes:

Antony Lee <antony.lee@...83...> writes:
It happens that this array has dtype=int16, and imshow's doc says that
it only accepts float arrays

Having looked at the example you sent me, I think what you want is .astype():

a = np.array([[24695, 19052, 0],
       [24565, 23793, -17393],
       [ 25883, -32082, -24333]], dtype=int16)
imshow(a) # all blue
imshow(a.astype(float)) # colours! :sunglasses:
imshow(a.astype(int)) # looks the same as using "float"
imshow(a.astype(float), interpolation="nearest") # without blurring

Hope this helps,
Jon Olav

asarray (and there’s no copy) works too, so my question now is rather, why does imshow cast (I suppose that’s what it does) 32bit arrays but not 16bit ones?

Thanks for your help.
Antony

···

just to put my previous message on the list in case someone is interested:

Save the data below to a file “dump” and then:
plt.imshow(np.loadtxt(“dump”, dtype=np.int16))

plt.show()

Compare with:

plt.imshow(abs(np.loadtxt(“dump”, dtype=np.int16)))

or

plt.imshow(np.asfarray(np.loadtxt(…)))

I’m using ipython -wthread.

Thanks in advance,

Antony

---- file begins here

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 -2.715000000000000000e+04

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 3.017200000000000000e+04 -2.077700000000000000e+04

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 2.701700000000000000e+04 -3.205700000000000000e+04 -1.202400000000000000e+04

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 2.955400000000000000e+04 2.663400000000000000e+04 -3.077400000000000000e+04 -1.526700000000000000e+04

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 2.631200000000000000e+04 2.371900000000000000e+04 2.469500000000000000e+04 1.905200000000000000e+04 0.000000000000000000e+00

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 1.997700000000000000e+04 2.029400000000000000e+04 1.944000000000000000e+04 2.456500000000000000e+04 2.379300000000000000e+04 -1.739300000000000000e+04

0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 1.982900000000000000e+04 1.571900000000000000e+04 1.890300000000000000e+04 2.588300000000000000e+04 -3.208200000000000000e+04 -2.433300000000000000e+04

---- file ends here

2010/5/6 Jon Olav Vik <jonovik@…287…>

Jon Olav Vik <jonovik@…83…> writes:

Antony Lee <antony.lee@…83…> writes:

It happens that this array has dtype=int16, and imshow’s doc says that

it only accepts float arrays

Having looked at the example you sent me, I think what you want is .astype():

a = np.array([[24695, 19052, 0],

   [24565, 23793, -17393],

   [ 25883, -32082, -24333]], dtype=int16)

imshow(a) # all blue

imshow(a.astype(float)) # colours! :sunglasses:

imshow(a.astype(int)) # looks the same as using “float”

imshow(a.astype(float), interpolation=“nearest”) # without blurring

Hope this helps,

Jon Olav



Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

It turns out that this is not a 16bit-32bit issue.
I think the culprit is how overflow is handled fro numpy integers.

For example,

im1 = imshow(np.loadtxt("dump", dtype=np.int16))
vmin, vmax = im1.norm.vmin, im1.norm.vmax
print vmax, vmin
print type(vmax), type(vmin)
print vmax - vmin

gives

30172 -32082
<type 'numpy.int16'> <type 'numpy.int16'>
-3282

And the normalization is incorrect.

For a workaround, try something like below.

im1 = imshow(np.loadtxt("dump", dtype=np.int16))
im1.set_clim(map(float, im1.get_clim()))

For how to fix this, I'm not quite clear what is the best way.
My inclination is to cast the min, max to float by default, i.e.,
in Normalize.autoscale and Normalizr.autoscale_None (in colors.py),
change ma.minimum(A) to float(ma.minimum(A)).

but I'll leave it to other developers. Just in case, I filed a bug.

http://sourceforge.net/tracker/?func=detail&aid=2997687&group_id=80706&atid=560720

Regards,

-JJ

···

On Thu, May 6, 2010 at 10:18 AM, Antony Lee <antony.lee@...1839...> wrote:

asarray (and there's no copy) works too, so my question now is rather, why
does imshow cast (I suppose that's what it does) 32bit arrays but not 16bit
ones?
Thanks for your help.
Antony