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

如何制作含healpy projscatter天空散点子图的2×2布局图形?

实现2×2布局的Healpy天空散点子图

可以通过Matplotlib的子图布局结合healpy的projscatter函数实现需求,核心是给projscatter指定对应的子图轴对象(ax参数)。以下是完整可运行的代码:

import numpy as np
import healpy as hp
import matplotlib.pyplot as plt

# 生成模拟的天球坐标数据(RA/DEC,单位:度)
np.random.seed(42)
n_points = 500
ra = np.random.uniform(0, 360, n_points)
dec = np.random.uniform(-90, 90, n_points)
# 转换为healpy需要的theta(极角,90-DEC)和phi(方位角,RA)
theta = np.radians(90 - dec)
phi = np.radians(ra)

# 创建2×2的子图布局
fig, axes = plt.subplots(2, 2, figsize=(12, 10), subplot_kw={'projection': 'mollweide'})
axes = axes.flatten()  # 把2×2的轴数组展平为一维,方便循环

# 定义每个子图的样式参数(颜色、标题)
plot_params = [
    {'color': 'red', 'title': 'Sky Scatter Plot 1'},
    {'color': 'blue', 'title': 'Sky Scatter Plot 2'},
    {'color': 'green', 'title': 'Sky Scatter Plot 3'},
    {'color': 'purple', 'title': 'Sky Scatter Plot 4'}
]

# 循环绘制每个子图
for ax, params in zip(axes, plot_params):
    # 在指定子图轴上绘制projscatter
    hp.projscatter(theta, phi, color=params['color'], s=10, ax=ax)
    # 设置子图标题
    ax.set_title(params['title'], y=1.05)
    # 隐藏默认的 Mollweide 坐标标签(可选,根据需求调整)
    ax.set_xticklabels([])
    ax.set_yticklabels([])

# 调整子图间距,避免标题重叠
plt.tight_layout()
plt.show()

关键说明:

  • subplot_kw={'projection': 'mollweide'}:指定子图使用莫尔韦德投影(和healpy默认投影匹配)
  • hp.projscatter的ax参数:将绘图绑定到指定的Matplotlib子图轴上,这是实现多子图的核心
  • 坐标转换:healpy使用极角theta(从北天极向下度量,范围0-π弧度)和方位角phi(从本初子午线向东度量,范围0-2π弧度),因此需要将常用的RA/DEC转换为这两个参数
  • 样式自定义:可以通过调整plot_params里的颜色、点大小(s参数)、标题等,实现每个子图的差异化展示

如果需要其他投影类型(如Gnomonic、Cartesian),只需修改subplot_kw里的projection参数,并确保hp.projscatter的projection参数与之匹配即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 04:17:34