0

I am trying to plot .npy-files containing 3d arrays (values in range from -90 to 315) with matplotlib and I am constantly getting an almost completely white image, with only a few colorful spots.

This is my code:

file = "file.npy"

img_array = np.load(file)

img = (img_array - img_array.min()) / (img_array.max() - img_array.min()) * 255

plt.imshow(img, vmin=0, vmax=255)
plt.show()

And this is the result:

enter image description here

Does anyone know why that is and how I can plot my image properly? Thank you!

Solution: The values in my array were of dtype float32. Once I changed them to dtype int8 it worked!

liz
  • 25
  • 2
  • Have you checked the matrix that you end up with after your calculations, i.e. ```img```? If these are not between 0 and 255, something is wrong with your file or calculation. – Tempman383838 May 02 '23 at 09:48
  • After normalizing the img_array the values are all in range 0-255, most of them somewhere between 100 and 200, with min = 0 and max = 255. – liz May 02 '23 at 12:27
  • You are plotting the wrong axis of your matrix, I suspect. Without knowing your ```file.npy```, I cannot help you. – Tempman383838 May 02 '23 at 12:40
  • I deleted my previous comment, my apologies. Try slicing your array with ```plt.imshow(img[0,:,:])```. – Tempman383838 May 02 '23 at 12:45
  • Just figured out the issue! See edit on the post. – liz May 02 '23 at 12:58
  • With a 3D array, when of type int, a range of 0-255 is used for the rgb values, for floats a range of 0-1 is used. `vmin` and `vmax` are ignored when rgb values are given. – JohanC May 02 '23 at 20:15

0 Answers0