使用 matplotlib 方向错误的 FITS 文件绘制的图像

Image plotted from a FITS file with matplotlib oriented incorrectly

我在使用 matplotlibimshow 绘制适合图像方面遇到了一些问题。看来我的图像在水平和垂直方向上都翻转了。我确定我忽略了一些简单的事情,如果有人能指出我正确的方向,那就太好了。

我的图片应该是这样的:

所以,我将图片加载为:

from astropy.io import fits
import matplotlib
import matplotlib.pyplot as pyplot
#Opening/reading in my fits file
hdulist = fits.open('.../myfits.fits')
#Accessing the image data and specifying the dimensions I wish to use
my_image = hdulist[0].data[0,0:,0:]
#Plotting the image
pyplot.imshow(image_SWIFT_uvm2_plot, cmap='gray', vmin=0, vmax=0.5)
pyplot.show()

这就是我在情节中的形象(情节比我包含的代码复杂一点,但我已经给出了关键线,希望是一个自给自足的代码):

眼尖的应该看出来了,图片左右翻转了。

我从未使用过 astropy 模块,但我知道 PyFITS 将图像数据作为 NumPy 数组打开(并且我 reading, astropy.io.fits has inherited the functionality of PyFITS anyway, so it should work the same way). If that is the case, then you may use numpy.fliplr and numpy.flipud 将数组翻转到您想要的方向。只需更换行

pyplot.imshow(image_SWIFT_uvm2_plot, cmap='gray', vmin=0, vmax=0.5)

import numpy as np
pyplot.imshow(np.fliplr(np.flipud(image_SWIFT_uvm2_plot)), cmap='gray',
    vmin=0, vmax=0.5)

或者,您可以做一点线性代数来翻转它,或者只注意执行这两种翻转与使用 np.rot90 两次

相同
pyplot.imshow(np.rot90(image_SWIFT_uvm2_plot, k=2), cmap='gray', vmin=0, vmax=0)

对于 FITS 文件,惯例是原点位于图像的左下角,因此您需要使用 origin='lower'(默认情况下 Matplotlib 使用 origin='upper')。