IPython FITS文件绘图给出了不同的结果

时间:2016-07-01 15:26:07

标签: python matplotlib astropy

我在运行脚本时遇到问题(请参阅下面的代码)。

我正在尝试绘制一个值数组,将其写入FITS文件格式,再次读回并绘制它--->我没有得到相同的情节!

如果你能帮我解决这个问题,那就太好了。

以下是我的软件包和编译器的版本:

matplotlib:' 2.0.0b1'

numpy:' 1.11.0'

astropy:u' 1.1.2'

python:2.7

此致 Anik Halder

import numpy as np

from pylab import *

from astropy.io import fits

# Just making a 10x10 meshgrid
x = np.arange(10)
X , Y = np.meshgrid(x,x)

# finding the distance of different points on the meshgrid from a point suppose at (5,5)
Z = ((X-5)**2 + (Y-5)**2)**0.5 

# plotting Z (see image [link below] - left one)
imshow(Z, origin = "lower")
colorbar()
show()

# writing the Z data into a fits file
fits.writeto("my_file.fits", Z)

# reading the same fits file and storing the data
Z_read = fits.open("my_file.fits")[0].data

# plotting Z_read : we expect it to show the same plot as before
imshow(Z_read, origin = "lower")
colorbar()
show()

# Lo! That's not the case for me! It's not the same plot! (see image - right one)

# Hence, I try to check whether the values stored in Z and Z_read are different..

print Z - Z_read

# No! It returns an array full of zeros! This means Z and Z_read are the same! I don't get why the plots look different!

请在此链接中找到图片:http://imgur.com/1TklSjU

1 个答案:

答案 0 :(得分:0)

实际上,事实证明它与matplotlib的版本有关。

matplotlib开发人员回答 - Jens Nielsen

在matplotlib 1.51版本上不会发生这种情况

在版本2 beta 1中,似乎FITS数据从float32转换为big endian float8。请查看以下链接:

https://gist.github.com/jenshnielsen/86d4a86d8f667fadddc09f88c5fb87e6

此问题已发布,您可以在此处查看:

https://github.com/matplotlib/matplotlib/issues/6671

在此期间,为了获得相同的图(Z和Z _read),我们应该使用以下代码(对于matplotlib 2 beta 1):

imshow(Z_read.astype('float64'), origin = "lower")