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

如何修改Matplotlib 3D绘图中指向Environment的两个向量箭头样式

解决方案:修改Matplotlib 3D箭头样式区分指向Environment的向量

需求说明

需要将指向Environment的两个向量箭头设为实心,其余向量箭头设为空心,以此突出视觉差异。

修改后的完整代码

import matplotlib.pyplot as plt
import numpy as np

# 定义向量
vectors = np.array([[0, 0, 18], [18, 0, 0], [0, 18, 0]])

# 原点
origin = np.array([0, 0, 0])

# 向量上的指定点
point1 = np.array(vectors[0])  # 对应History
point2 = np.array(vectors[1])  # 对应Adaptive Repertoire
point3 = np.array(vectors[2])  # 对应Environment

# 绘图初始化
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 向量颜色
colors = ['k', 'k', 'k']

# 绘制原点出发的向量
for i, (vector, color) in enumerate(zip(vectors, colors)):
    if i == 1:  # Adaptive Repertoire设为双向空心箭头
        ax.quiver(origin[0], origin[1], origin[2], vector[0], vector[1], vector[2],
                  arrow_length_ratio=0.1, color=color, arrowstyle='->,fill=False')
        ax.quiver(vector[0], vector[1], vector[2], -vector[0], -vector[1], -vector[2],
                  arrow_length_ratio=0.1, color=color, arrowstyle='->,fill=False')
    else:
        # History和Environment的原点侧设为单向空心箭头
        ax.quiver(origin[0], origin[1], origin[2], -vector[0], -vector[1], -vector[2],
                  arrow_length_ratio=0.1, color=color, pivot='tip', arrowstyle='->,fill=False')

# 绘制点之间的连接箭头
x_points = [point1[0], point2[0], point3[0]]
y_points = [point1[1], point2[1], point3[1]]
z_points = [point1[2], point2[2], point3[2]]

# 遍历所有点对
for i in range(3):
    for j in range(i + 1, 3):
        # 计算双向箭头方向
        dir_ij = np.array([x_points[j]-x_points[i], y_points[j]-y_points[i], z_points[j]-z_points[i]])
        dir_ji = -dir_ij
        
        # 绘制i指向j的箭头:若j是Environment则用实心,否则空心
        if j == 2:
            ax.quiver(x_points[i], y_points[i], z_points[i], dir_ij[0], dir_ij[1], dir_ij[2],
                      color='b', alpha=1, arrow_length_ratio=0.1, arrowstyle='->')
        else:
            ax.quiver(x_points[i], y_points[i], z_points[i], dir_ij[0], dir_ij[1], dir_ij[2],
                      color='b', alpha=1, arrow_length_ratio=0.1, arrowstyle='->,fill=False')
        
        # 绘制j指向i的箭头:若i是Environment则用实心,否则空心
        if i == 2:
            ax.quiver(x_points[j], y_points[j], z_points[j], dir_ji[0], dir_ji[1], dir_ji[2],
                      color='b', alpha=1, arrow_length_ratio=0.1, arrowstyle='->')
        else:
            ax.quiver(x_points[j], y_points[j], z_points[j], dir_ji[0], dir_ji[1], dir_ji[2],
                      color='b', alpha=1, arrow_length_ratio=0.1, arrowstyle='->,fill=False')

# 文本标签配置
text_content = [
    "History",
    "Adaptive Repertoire",
    "Environment"
]

text_coordinates = [
    (5, 5, 30),
    (-15, 25, -5),
    (15, -20, -15)
]

# 添加文本框
for text, (x, y, z) in zip(text_content, text_coordinates):
    ax.text(x, y, z, text, fontsize=10, color='black', ha='center', va='center',
            bbox=dict(boxstyle='round', facecolor='white', alpha=0))

# 设置坐标轴范围
ax.set_xlim([-20, 20])
ax.set_ylim([-20, 20])
ax.set_zlim([-20, 20])

# 坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 调整视角
ax.view_init(elev=29, azim=45)

plt.show()

关键修改说明

  1. 箭头样式控制:
    • 实心箭头使用默认样式arrowstyle='->'(自动填充箭头头部)
    • 空心箭头通过arrowstyle='->,fill=False'取消头部填充
  2. 目标向量判断:
    • 检查连接箭头的目标点是否为Environment对应的索引(i == 2或j == 2),符合条件则应用实心样式
  3. 统一基础样式:将原点出发的所有基础向量设为空心,确保只有指向Environment的连接箭头为实心,强化视觉对比

内容的提问来源于stack exchange,提问作者Maud van Lier

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 12:54:55