0

I am running into a problem viewing images with astropy. Here is my code:

from astropy.io import fits
import matplotlib.pyplot as plt
hdu_list=fits.open("500m2deep.fit")
image_data=hdu_list[0].data
hdu_list.close()
plt.imshow(image_data,cmap='gray')
plt.show()

Opening the file works fine, I can display the entries of image_data and alike. But the picture doesn't show if I use imshow. It displays the following error:

C:\Python27\lib\site-packages\IPython\core\formatters.py:239: FormatterWarning: Exception in image/png formatter: FormatterWarning,

If I use, as suggested on some sites, %matplotlib inline, or something similar, this error disappears, but no image shows at all, the program runs, terminates, no picture pops up. I also tried adding something like plt.figure() before imshow() but that doesn't help either.

This happens if I use Spyder, Ipython, or Ipython Notebook. I am using the newest version of python(x,y) for all of this.

How can I display the pictures?

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
BlackyTheCat
  • 343
  • 1
  • 2
  • 9
  • Is there anything more to that warning? Seems odd that it would be so unspecific. It sounds like maybe there's something wrong with your matplotlib and/or IPython configuration. Instead of calling `plt.show()` what happens if you call `plt.savefig('img.png')` and write the image out to a PNG, then open that? From the code sample you gave above there's nothing wrong or unusual about anything you're doing. – Iguananaut Oct 22 '15 at 22:21
  • In particular, this *probably* has nothing to do with astropy. You can rule this out by just doing something like `plt.imshow(numpy.arange(10000).reshape(100, 100)); plt.show()`. Should give you a rainbow. If the image simply isn't displaying there's probably something wrong with communication between IPython and matplotlib on your system. – Iguananaut Oct 22 '15 at 22:25
  • Is there any chance you can make the FITS image available, so other people can try and see if they can recreate the problem? –  Oct 23 '15 at 04:47
  • After the formatting error, I sometimes get a "matplot.figure() at..."and then some numbers. If I use savefig I get a memory error. I have over 4 Gb available though (8 in total, but 4 should not be in use). Could the application use more than 4 Gb? The rainbow color picture display works, so I think it might be that the file actually uses up more than 4 Gb. – BlackyTheCat Oct 26 '15 at 08:18

1 Answers1

0

Maybe too late to be a useful solution for you, but mayb someone else can profit. I recently ran into the same issue on Ubuntu 16.04, python 3.5 and Astropy 1.1.1 using the sample code from the astropy website (first and last line added by me), which is:

#!/usr/bin/python3
import matplotlib.pyplot as plt
from astropy.wcs import WCS
from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

filename = get_pkg_data_filename('galactic_center/gc_msx_e.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)
plt.subplot(projection=wcs)
plt.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
plt.grid(color='white', ls='solid')
plt.xlabel('Galactic Longitude')
plt.ylabel('Galactic Latitude')
plt.show()

It did open the figure window, but did not display any image data. Updating the astropy version to 1.3 (which also updated numpy to 1.11.3) fixed the issue. Now it works fine.