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

如何基于天文FITS图像数据计算散点图标记的s参数以匹配实际尺寸?

在天文FITS图像上按天球坐标绘制匹配尺寸的空心圆

基于你提供的FITS图像绘制代码,要在指定天球坐标处绘制尺寸匹配的空心圆,核心是完成天球坐标转像素坐标和根据角大小计算scatter的s参数两个步骤,具体实现如下:

关键步骤说明

  1. 天球坐标转像素坐标:利用astropy.wcs.WCS的world_to_pixel方法,将带单位的RA/Dec转换为图像的像素坐标(x,y)。
  2. 计算像素比例尺:通过WCS获取当前位置每像素对应的角大小,将给定的角尺寸(25角分)转换为像素直径。
  3. 计算scatter的s参数:matplotlib.pyplot.scatter的s参数表示点的平方像素面积,因此需要根据像素直径计算圆的面积。

修改后的完整代码

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

hdul = fits.open("test.fits")
img = hdul[0].data
hdr = hdul[0].header
wcs = WCS(hdr)

def displayimage(img, wcs):
    fig, ax = plt.subplots(figsize=(5, 5), subplot_kw={"projection": wcs.celestial})
    norm = simple_norm(img, vmin=0, vmax=np.nanmax(img), stretch="linear")
    im = ax.imshow(img, norm=norm, cmap="viridis", origin="lower")
    
    # 定义目标天球坐标和圆的角大小
    ra_target = 120 * u.deg
    dec_target = 30 * u.deg
    circle_size = 25 * u.arcmin
    
    # 天球坐标转像素坐标
    x_pix, y_pix = wcs.world_to_pixel(ra_target, dec_target)
    
    # 计算像素比例尺(弧度/像素转角分/像素)
    pix_scale_rad = wcs.proj_plane_pixel_scales()[0]
    pix_scale_arcmin = pix_scale_rad * u.rad.to(u.arcmin)
    
    # 计算圆的像素直径
    pixel_diameter = circle_size.value / pix_scale_arcmin
    
    # 计算scatter的s参数(圆面积=π*(半径)^2)
    circle_area = np.pi * (pixel_diameter / 2) ** 2
    
    # 绘制空心圆:facecolor='none'设为空心,edgecolor设置边框颜色
    ax.scatter(x_pix, y_pix, s=circle_area, facecolor='none', edgecolor='red', linewidth=2)
    
    # 设置坐标轴标签
    lon = ax.coords[0]
    lat = ax.coords[1]
    lon.set_axislabel("RA")
    lat.set_axislabel("Dec")
    
    plt.show()

displayimage(img, wcs)

代码细节解释

  • 单位处理:导入astropy.units确保坐标和尺寸的单位统一转换,避免计算错误。
  • 像素比例尺:proj_plane_pixel_scales()返回WCS投影平面上的像素比例尺(默认单位是弧度),转换为角分后,就能直接用角尺寸除以比例尺得到像素直径。
  • 空心圆设置:通过facecolor='none'将圆设置为空心,edgecolor和linewidth调整边框样式,确保在图像上清晰可见。

内容的提问来源于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.12 15:17:09