python - 显示具有多个 channel 的拟合图像

标签 python python-3.x matplotlib astropy fits

Here is my expected result

我从 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 网格之外,我还没有格式化图像。以下是此示例代码的输出图像:enter image description here

关于python - 显示具有多个 channel 的拟合图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57383130/

相关文章:

python - CSV 阅读器在前几个字符中捡垃圾

python - 我如何绘制分成类别的数据的相对频率?

python - 改变图像的深度?

python - 如何使用 python 将代理设置/凭据传递到命令提示符,以便同一 python 代码中的后续命令可以访问互联网?

python - 如何获取y_true和y_pred之差的水平和垂直梯度?

python - 以编程方式阻止特定文件(.txt 扩展名)的打印请求?

python - Django SplitDateTime 小部件抛出 'list' 对象没有属性 'strip'

python - 改造蛇需要关于如何使蛇更长并与更长的蛇一起移动的想法

Python Matplotlib 直方图 bin 移位

python - 从包含 QString 的变量生成 unicode