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

如何让Matplotlib保存的图表含额外信息且交互式图表仅显轨道

解决方案

核心思路是分离交互式展示与保存的图表内容,通过两种方式实现:

方式一:创建两个独立图表(无阻塞,可同时操作)

先绘制仅含轨道和图例的交互式图表,再基于相同数据绘制带额外信息的图表并保存,两者完全独立。

import matplotlib.pyplot as plt

# 行星轨道数据(替换为你turtle模拟的实际AU值和总地球年)
planets = {
    "Venus": {"au": 0.723, "color": "gold"},
    "Earth": {"au": 1.0, "color": "blue"},
    "Mars": {"au": 1.524, "color": "red"},
    "Jupiter": {"au": 5.203, "color": "orange"},
}
total_earth_years = 10  # 你的模拟总地球年数

# ---------------------- 绘制交互式图表(仅轨道+图例) ----------------------
fig_interactive, ax_interactive = plt.subplots(figsize=(8, 8))
ax_interactive.set_aspect("equal")
ax_interactive.set_title("Planetary Orbits")
ax_interactive.set_xlabel("AU")
ax_interactive.set_ylabel("AU")

# 绘制太阳
ax_interactive.scatter(0, 0, color="yellow", s=200, label="Sun")

# 绘制行星轨道
for name, data in planets.items():
    circle = plt.Circle((0, 0), data["au"], color=data["color"], fill=False, linewidth=2)
    ax_interactive.add_artist(circle)
    ax_interactive.plot([], [], color=data["color"], linewidth=2, label=name)

ax_interactive.legend()
# 弹出交互式窗口(block=False不阻塞后续代码执行)
plt.show(block=False)

# ---------------------- 绘制带额外信息的图表并保存 ----------------------
fig_save, ax_save = plt.subplots(figsize=(8, 8))
ax_save.set_aspect("equal")
ax_save.set_title("Planetary Orbits with Details")
ax_save.set_xlabel("AU")
ax_save.set_ylabel("AU")

# 复制基础绘制内容
ax_save.scatter(0, 0, color="yellow", s=200, label="Sun")
for name, data in planets.items():
    circle = plt.Circle((0, 0), data["au"], color=data["color"], fill=False, linewidth=2)
    ax_save.add_artist(circle)
    ax_save.plot([], [], color=data["color"], linewidth=2, label=name)

ax_save.legend()

# 添加AU数值和总地球年信息
info_text = (
    "Orbit Details:\n"
    + "\n".join([f"{name}: {data['au']} AU" for name, data in planets.items()])
    + f"\n\nTotal Simulation Time: {total_earth_years} Earth Years"
)
# 在图表右侧添加带背景的文本框
ax_save.text(
    1.05,
    1.0,
    info_text,
    transform=ax_save.transAxes,
    bbox=dict(facecolor="white", alpha=0.8),
    verticalalignment="top",
)

# 保存图表(bbox_inches='tight'防止文本被截断)
plt.savefig("planetary_orbits_details.png", bbox_inches="tight")
plt.close(fig_save)

# 等待交互式窗口关闭后结束程序
plt.show()

方式二:先展示基础图表,关闭后修改保存

先弹出仅含轨道和图例的交互式窗口,用户关闭后,在同一个画布上添加额外信息并保存,避免重复绘制轨道。

import matplotlib.pyplot as plt

planets = {
    "Venus": {"au": 0.723, "color": "gold"},
    "Earth": {"au": 1.0, "color": "blue"},
    "Mars": {"au": 1.524, "color": "red"},
    "Jupiter": {"au": 5.203, "color": "orange"},
}
total_earth_years = 10

# 绘制基础图表
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_aspect("equal")
ax.set_title("Planetary Orbits")
ax.set_xlabel("AU")
ax.set_ylabel("AU")

ax.scatter(0, 0, color="yellow", s=200, label="Sun")
for name, data in planets.items():
    circle = plt.Circle((0, 0), data["au"], color=data["color"], fill=False, linewidth=2)
    ax.add_artist(circle)
    ax.plot([], [], color=data["color"], linewidth=2, label=name)

ax.legend()

# 弹出交互式窗口,程序暂停直到用户关闭窗口
plt.show()

# 修改图表,添加额外信息
ax.set_title("Planetary Orbits with Details")
info_text = (
    "Orbit Details:\n"
    + "\n".join([f"{name}: {data['au']} AU" for name, data in planets.items()])
    + f"\n\nTotal Simulation Time: {total_earth_years} Earth Years"
)
ax.text(
    1.05,
    1.0,
    info_text,
    transform=ax.transAxes,
    bbox=dict(facecolor="white", alpha=0.8),
    verticalalignment="top",
)

# 保存修改后的图表
plt.savefig("planetary_orbits_details.png", bbox_inches="tight")
plt.close(fig)

关键说明

  • 两种方式都避免了“展示与保存内容绑定”的问题:方式一通过双图表实现并行操作,方式二利用plt.show()的阻塞特性,在用户关闭展示窗口后再修改保存。
  • 文本框使用transform=ax.transAxes确保位置相对图表固定,不会随坐标轴缩放偏移;bbox参数添加半透明白色背景,提升可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 05:43:24