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

如何用Matplotlib在一张图中叠加绘制两张地图?(Python)

解决FITS地图叠加显示问题

你的代码存在两个核心问题导致叠加效果不显示:

  1. 变量名错误:代码中使用了未定义的texture变量,实际应该用你读取的PSI_MAP数据pl_b
  2. 图层顺序错误:先绘制了带透明度的图层,随后绘制的不透明图层直接覆盖了它

修正后的代码

import matplotlib.pyplot as plt
from astropy.io import fits  # 或你使用的fits读取库

input_file = "slicedSmoothedStokesPlanck/Smoothed_Sliced_PSI_MAP.fits"
i_file = "slicedSmoothedStokesPlanck/Smoothed_Sliced_I_MAP.fits"

# 读取两张FITS图像数据
pl_b = fits.getdata(input_file, ext=0)
i_file_data = fits.getdata(i_file, ext=0)

fig = plt.figure()
ax = fig.add_subplot(111)

# 第一步:绘制底层地图(map1,示例用I_MAP)
ax.imshow(i_file_data)
# 第二步:绘制上层地图(map2,PSI_MAP),设置透明度alpha=0.5
ax.imshow(pl_b, alpha=0.5)

plt.title("TEST: PSI_MAP 叠加在 I_MAP 上")
plt.show()

额外注意事项

  • 确认两张地图的尺寸完全匹配,可以通过print(pl_b.shape, i_file_data.shape)检查,尺寸不一致会导致叠加错位或显示异常
  • 可以给上层地图指定不同的cmap(比如cmap='RdYlBu_r'),避免和底层地图颜色混淆,提升叠加辨识度
  • 如果需要WCS投影,取消# projection=wcs的注释,并确保已正确初始化对应的WCS对象

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 10:57:22