我从 CASA 下载了具有多个 channel 的拟合图像。我尝试像正常的适合图像一样上传图像,但它显示错误
"Invalid dimensions for image data".
图像的形状为 (1, 20, 250, 250)。
有没有办法显示所有 channel ?
当我尝试下面的代码时,它只显示其中一个 channel 。
file2 = "Downloads/PVDiagramtest2.fits"
image_data = fits.getdata(file2)
image_data = image_data[~np.isnan(image_data)]
plt.figure()
plt.imshow(image_data[0,0,:,:])
plt.show()
最佳答案
要查看所有图像,您可以循环查看子图。如何将多个图像绘制在一起的玩具示例如下所示:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)
for i in range(16):
plt.subplot(4, 4, i+1)
plt.scatter(x, y, s=80, c=z, marker=verts)
plt.show()
就您而言,我认为可能看起来像这样:
from astropy.io import fits
# import numpy as np
import matplotlib.pyplot as plt
file = "WFPC2u5780205r_c0fx.fits"
image_data = fits.getdata(file)
# image_data = image_data[~np.isnan(image_data)]
num_channels = 4
x_dim = 2
y_dim = 2
colors = ['rainbow', 'PuRd_r', 'gist_earth', 'coolwarm']
for i in range(num_channels):
plt.subplot(x_dim, y_dim, i+1)
plt.imshow(image_data[i,:,:], cmap=colors[i])
plt.show()
值得注意的是,我注释掉了您在 np.isnan()
上进行位翻转的位置,因为它似乎正在压平图像数组,而且我没有看到使用这种类型的合理性当它似乎只是引入一个问题时的方法。但是,也许对于您的数据,它的行为会如您所愿。
对于此示例,我使用了 FITS Support Office 中提供的第一个示例 FITS 文件。 。该图像为 4 channel 200x200 像素。除了使用子图制作 2x2 网格之外,我还没有格式化图像。以下是此示例代码的输出图像:
关于python - 显示具有多个 channel 的拟合图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57383130/