[英]Image plotted from a FITS file with matplotlib oriented incorrectly
I'm having a little issue with something regarding plotting a fits image using matplotlib
's imshow
. 关于使用matplotlib
的imshow
绘制适合图像的问题,我遇到了一点问题。 It seems that my image is flipped both horizontally and vertically. 看来我的图像在水平和垂直方向都被翻转了。 I'm sure there is something simple I am overlooking, if anyone could point me in the right direction that would be great. 我敢肯定,如果有人能指出正确的方向,那我会忽略一些简单的事情,那就太好了。
This is what my image should look like: 这是我的图像应如下所示:
So, I'm loading my image as: 因此,我将图像加载为:
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()
This is what my image in the plot looks like (the plot is a little more complex than the code I have included, but I have given the critical lines as, hopefully, a self-sufficient code): 这是我在绘图中的图像的样子(绘图比我包含的代码稍微复杂一点,但是我给出的关键行希望是自给自足的代码):
Those of you with keen eyes should see that the image has flipped both horizontally and vertically. 那些敏锐的眼睛应该看到图像水平和垂直翻转。
对于FITS文件,约定是原点位于图像的左下角,因此您需要使用origin='lower'
(默认情况下Matplotlib使用origin='upper'
)。
I have never used the astropy module, but I know that PyFITS opens the image data as a NumPy array (and from what I'm reading , astropy.io.fits has inherited the functionality of PyFITS anyway, so it should work the same way). 我从未使用过astropy模块,但是我知道PyFITS将图像数据作为NumPy数组打开(并且从我正在阅读的内容中 ,astropy.io.fits继承了PyFITS的功能,因此它应该以相同的方式工作)。 If that is the case, then you may use numpy.fliplr
and numpy.flipud
to flip the array to your desired orientation. 如果是这种情况,则可以使用numpy.fliplr
和numpy.flipud
将数组翻转到所需的方向。 Just replace the line 只需更换线
pyplot.imshow(image_SWIFT_uvm2_plot, cmap='gray', vmin=0, vmax=0.5)
with 与
import numpy as np
pyplot.imshow(np.fliplr(np.flipud(image_SWIFT_uvm2_plot)), cmap='gray',
vmin=0, vmax=0.5)
Alternatively, you could do a little linear algebra to flip it, or just note that performing both of these flips is the same as using np.rot90
twice 另外,您可以做一些线性代数来翻转它,或者只是注意执行两次翻转与两次使用np.rot90
相同
pyplot.imshow(np.rot90(image_SWIFT_uvm2_plot, k=2), cmap='gray', vmin=0, vmax=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.