You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改matplotlib子图RA轴单位从时分秒格式转为度单位?

解决方案

你遇到的两个问题原因如下:

  • AttributeError: 'AxesSubplot' object has no attribute 'coords'报错是因为未正确获取带WCS投影的坐标轴对象,普通Matplotlib坐标轴没有coords属性
  • 之前的单位设置逻辑错误,set_format_unit的参数需要填写目标单位,而非坐标类型ra

正确实现代码

首先确保导入必要依赖:

from astropy.io import fits
from astropy.wcs import WCS
import matplotlib.pyplot as plt
import astropy.units as u

完整绘图逻辑:

# 读取FITS文件与WCS坐标信息
hdu = fits.open('flame.fits')[0]
wcs = WCS(hdu.header)

# 创建带WCS投影的坐标轴
fig = plt.figure()
ax = fig.add_subplot(111, projection=wcs)

# 绘制图像
ax.imshow(hdu.data, origin='lower', cmap=plt.cm.viridis)

# 修改RA轴单位为十进制度
ra_axis = ax.coords['ra']
ra_axis.set_format_unit(u.degree)
# 可选:调整刻度显示的小数位数,示例为保留2位小数
# ra_axis.set_major_formatter('d.dd')

# 设置坐标轴标签
ax.set_xlabel('RA (deg)')
ax.set_ylabel('Dec')

plt.show()

高版本astropy也可以直接传入字符串'degree'替换u.degree,无需额外导入astropy.units,效果一致。


内容的提问来源于stack exchange,提问作者Daisy_Stoker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 21:48:03