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

如何修改Astropy曲轴的RA网格间隔及Dec标签显示

问题描述

我用以下Python代码基于Astropy处理FITS天文数据,绘制带WCS投影的图像并添加ICRS坐标系叠加网格:

import astropy.units as u
import numpy as np
from astropy.io import fits
from astropy.wcs import WCS
import matplotlib.pyplot as plt
from astropy.visualization import simple_norm
from astropy.coordinates import SkyCoord, Galactic

file = "hi4pi-hvc-vlsr-gal-car.fits"
hdul = fits.open(file)
hdr = hdul[0].header
data = hdul[0].data
wcs = WCS(hdr)
norm = simple_norm(data, "linear")

# Plot image on axes ax
fig, ax = plt.subplots(figsize=(10, 6), subplot_kw={"projection": wcs})
cmap = plt.get_cmap("RdBu", 20)
im = ax.imshow(data, cmap=cmap)
ax.set_xlabel("Galactic Longitude")
ax.set_ylabel("Galactic Latitude")

overlay = ax.get_coords_overlay("icrs")
major_ticks = np.arange(0, 360, 30)
#ax.set_xticks(major_ticks)
overlay.grid(color="grey", ls="dotted")
plt.colorbar(im, orientation="horizontal")

plt.savefig("area.png", dpi=300, bbox_inches="tight")
plt.show()

当前RA网格间隔为90度,我希望将其设置为30度(即使用np.arange(0,360,30));同时希望将图像右侧的dec标签修改为Dec,但查阅Astropy相关文档后,未找到调整overlay网格间隔及修改标签的方法,特此求助。

附:FITS文件来源为Westmeier 2018,其WCS关键字信息如下:

WCS Keywords

Number of WCS axes: 2
CTYPE : 'GLON' 'GLAT' 
CRVAL : np.float64(180.000000024) np.float64(0.0) 
CRPIX : np.float64(2162.0) np.float64(1080.0) 
PC1_1 PC1_2  : np.float64(1.0) np.float64(0.0) 
PC2_1 PC2_2  : np.float64(0.0) np.float64(1.0) 
CDELT : np.float64(-0.0833333333) np.float64(0.0833333333) 
NAXIS : 4323  2144
解决方案

1. 调整ICRS坐标系RA网格间隔到30度

overlay对象包含两个坐标组件(RA和Dec),按顺序overlay[0]对应RA,overlay[1]对应Dec。给RA坐标组件调用set_ticks方法,传入带单位的刻度数组即可:

# 设置RA刻度间隔为30度
overlay[0].set_ticks(np.arange(0, 360, 30) * u.degree)

注意必须给刻度数组加上u.degree单位,Astropy的WCS坐标组件需要明确单位才能正确解析刻度位置。

2. 修改右侧Dec标签为Dec

通过overlay[1]访问Dec坐标组件,调用set_axislabel方法修改标签文本:

# 修改Dec轴标签
overlay[1].set_axislabel("Dec")

修改后的完整代码

import astropy.units as u
import numpy as np
from astropy.io import fits
from astropy.wcs import WCS
import matplotlib.pyplot as plt
from astropy.visualization import simple_norm
from astropy.coordinates import SkyCoord, Galactic

file = "hi4pi-hvc-vlsr-gal-car.fits"
hdul = fits.open(file)
hdr = hdul[0].header
data = hdul[0].data
wcs = WCS(hdr)
norm = simple_norm(data, "linear")

# Plot image on axes ax
fig, ax = plt.subplots(figsize=(10, 6), subplot_kw={"projection": wcs})
cmap = plt.get_cmap("RdBu", 20)
im = ax.imshow(data, cmap=cmap)
ax.set_xlabel("Galactic Longitude")
ax.set_ylabel("Galactic Latitude")

overlay = ax.get_coords_overlay("icrs")
# 设置RA刻度间隔为30度
overlay[0].set_ticks(np.arange(0, 360, 30) * u.degree)
# 修改Dec轴标签
overlay[1].set_axislabel("Dec")
overlay.grid(color="grey", ls="dotted")
plt.colorbar(im, orientation="horizontal")

plt.savefig("area.png", dpi=300, bbox_inches="tight")
plt.show()

内容的提问来源于stack exchange,提问作者Firestar-Reimu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 05:44:53