python - Matplotlib 3D Quiver 图使线条颜色正确,但箭头颜色错误

标签 python matplotlib

我正在尝试在 x、y 和 z 方向上绘制三个箭头的箭袋图,箭头颜色为绿色、红色和蓝色。出于某种原因,线条颜色正确,但箭头颜色错误,我不知道如何修复。这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.gca(projection='3d')
cols = ['r', 'g', 'b']

quivers = ax.quiver([0,0,0],[0,0,0],[0,0,0],[1,0,0],[0,1,0],[0,0,1], colors=cols)

ax.set_xlim3d([-2.0, 2.0])
ax.set_xlabel('X')

ax.set_ylim3d([-2.0, 2.0])
ax.set_ylabel('Y')

ax.set_zlim3d([-2, 2])
ax.set_zlabel('Z')
plt.show()
Quiver plot

最佳答案

quiver , 如果颜色列表被指定为选项 colors=.. ,颜色循环对所有线段有效,包括箭头的所有部分。
要获得 3 个不同颜色的箭头,可以使用 3 quiver声明,每一个都有不同的颜色。
方法一

ax.quiver([0],[0],[0],[1],[0],[0], colors='r')
ax.quiver([0],[0],[0],[0],[1],[0], colors='g')
ax.quiver([0],[0],[0],[0],[0],[1], colors='b')
方法 2
fr = [(0,0,0), (0,0,0), (0,0,0)]
to = [(1,0,0), (0,1,0), (0,0,1)]
cr = ["red", "green", "blue"]
for (from_, to_, colr_) in zip(fr,to,cr):
    ax.quiver(*from_, *to_, colors=colr_)

关于python - Matplotlib 3D Quiver 图使线条颜色正确,但箭头颜色错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65039988/

相关文章:

python - 为 jupyter 笔记本启用 python-markdown 扩展时出错

python - 值错误 : readline of closed file in Python

python - 使用 Emacs Jedi(在 Anaconda 中)时可以避免使用 virtualenv 吗?

python - 给定所述元组中的项目,获取浅元组/列表的索引

python - 我应该以哪种格式保存我的 python 脚本输出?

python - 来自 Pandas DataFrame 的基本 Matplotlib 散点图

python - 在 matplotlib 中绘制均值

Matplotlib Axis 具有两个比例共享原点

python - 有没有办法优雅地在圆圈内画一个箭头

python - sklearn.plot_tree 如何可视化分类任务的 class_labels?