我正在matplotlib
从一些.fits
文件中以绘制一些星系速度的图。问题在于图中的轴以像素为单位显示了银河系的大小,我想将它们显示为Declination和RightAcension(以角度为单位)。我已经知道每个像素的大小为0.396弧秒。如何在X和Y轴上将像素转换为弧秒?
代码如下:
##############################################################################
# Generally the image information is located in the Primary HDU, also known
# as extension 0. Here, we use `astropy.io.fits.getdata()` to read the image
# data from this first extension using the keyword argument ``ext=0``:
image_data = fits.getdata(image_file, ext=0)
##############################################################################
# The data is now stored as a 2D numpy array. Print the dimensions using the
# shape attribute:
print(image_data.shape)
##############################################################################
# Display the image data:
fig = plt.figure()
plt.imshow(image_data, cmap='Spectral_r', origin='lower', vmin=-maior_pixel, vmax=maior_pixel)
plt.colorbar()
fig.suptitle(f'{gals_header["MANGAID"]}', fontsize=20, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('RC')
ax.set_xlabel('pixelsx')
ax.set_ylabel('pixelsy')
除此之外,还有更多的代码,但是我只想显示我认为是相关的部分(如有必要,我可以将其更多地放在注释中)。该代码基于以下链接中的示例代码:https : //docs.astropy.org/en/stable/genic/examples/io/plot_fits-image.html#sphx-glr-download-Generated-examples-io-情节适合图像py
我已经尝试过类似的东西Axes.convert_xunits
和一些pyplot.axes
功能,但是没有任何效果(或者也许我只是想不出如何正确使用它们)。
有人可以帮忙吗?先感谢您。
您可以使用plt.FuncFormatter
对象将任何内容用作刻度标签。
这是一个示例(确实是一个非常愚蠢的示例),有关详细信息,请参考出色的Matplotlib文档。
import matplotlib.pyplot as plt
from numpy import arange
img = arange(21*21).reshape(21,21)
ax = plt.axes()
plt.imshow(img, origin='lower')
ax.xaxis.set_major_formatter(
plt.FuncFormatter(lambda x, pos: "$\\frac{%d}{20}$"%(200+x**2)))
每个轴都有一个major_formatter
负责生成刻度标签的轴。
格式化程序必须是的子类的实例,在Formatter
上面使用FuncFormatter
。
要初始化a,FuncFormatter
我们将其传递给格式化函数,我们必须使用以下必需的特征对其进行定义
x
和pos
,x
被横坐标(或纵坐标)要被格式化而pos
可以安全地被忽略,在示例中,该函数是使用lambda
语法在现场定义的,其要点是格式字符串("$\\frac{%d}{20}$"%(200+x**2)
),其格式为LaTeX
横坐标的一部分,如上图所示。
pos
据我所知,仅在某些方法中使用该参数,例如
In [69]: ff = plt.FuncFormatter(lambda x, pos: "%r ፨ %05.2f"%(pos,x))
In [70]: ff.format_ticks((0,4,8,12))
Out[70]: ['0 ፨ 00.00', '1 ፨ 04.00', '2 ፨ 08.00', '3 ፨ 12.00']
但是通常您可以忽略pos
函数主体中的参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句