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

Python实现蓝色类地行星边缘垂直向量a_h、a_r的Quiver图绘制问题

解决方案:用Matplotlib Quiver绘制行星边缘的垂直向量

没问题!我来帮你搞定这个需求——用quiver在蓝色圆形(模拟行星)的顶部/底部边缘画出互相垂直的向量a_h和a_r。下面是具体的实现思路和完整代码:

核心实现思路

  • 先用plt.Circle绘制蓝色圆形模拟行星,同时设置坐标轴等比例避免圆形变形
  • 确定行星边缘的关键坐标:顶部为(0, 行星半径),底部为(0, -行星半径)
  • 定义互相垂直的向量:水平向量a_h(比如向右)和径向向量a_r(顶部向上、底部向下,保证垂直且指向行星外侧)
  • 用plt.quiver绘制向量,通过参数精准控制向量的长度、方向和显示效果

完整代码示例

import matplotlib.pyplot as plt

# 1. 配置行星基础参数
planet_radius = 2
planet_color = '#1f77b4'  # 偏蓝的色调,接近地球外观
center_point = (0, 0)

# 2. 创建画布与坐标轴,确保比例均等(避免圆形变椭圆)
fig, ax = plt.subplots(figsize=(6, 6))
ax.set_aspect('equal')

# 3. 绘制蓝色行星
planet = plt.Circle(center_point, planet_radius, color=planet_color, alpha=0.7)
ax.add_patch(planet)

# 4. 定义顶部边缘的向量参数
top_edge_start = (0, planet_radius)
a_h = (1, 0)  # 水平向右的向量
a_r = (0, 1)  # 垂直向上(径向向外)的向量,与a_h垂直

# 5. 用Quiver绘制向量
# 绘制水平向量a_h,红色标注
ax.quiver(*top_edge_start, *a_h, color='red', scale=1, scale_units='xy', angles='xy', label=r'$\vec{a}_h$')
# 绘制径向向量a_r,绿色标注
ax.quiver(*top_edge_start, *a_r, color='green', scale=1, scale_units='xy', angles='xy', label=r'$\vec{a}_r$')

# 如果需要绘制底部边缘的向量,取消下面的注释即可
# bottom_edge_start = (0, -planet_radius)
# ax.quiver(*bottom_edge_start, *a_h, color='red', scale=1, scale_units='xy', angles='xy', label=r'$\vec{a}_h$ (bottom)')
# ax.quiver(*bottom_edge_start, (0, -1), color='green', scale=1, scale_units='xy', angles='xy', label=r'$\vec{a}_r$ (bottom)')

# 6. 调整图表显示效果
ax.set_xlim(-planet_radius - 2, planet_radius + 2)
ax.set_ylim(-planet_radius - 2, planet_radius + 2)
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_title('Blue Planet with Perpendicular Vectors at Top Edge')
ax.legend()
ax.grid(True, linestyle='--', alpha=0.5)

plt.show()

关键参数说明

  • ax.set_aspect('equal'):必须设置,否则圆形会因为坐标轴比例不均变成椭圆
  • plt.quiver的scale=1和scale_units='xy':让向量的长度直接对应坐标轴的单位,确保视觉上的长度准确
  • angles='xy':保证向量的方向完全按照输入的(dx, dy)值绘制,不会出现角度偏移
  • 底部向量的a_r需要设为(0, -1):因为底部的径向向外是远离中心,方向向下,才能和水平的a_h保持垂直

你可以根据需求调整行星半径、向量长度、颜色等参数,也可以同时开启顶部和底部的向量绘制~

内容的提问来源于stack exchange,提问作者Jac Maredudd Davies

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:15:16