我正在尝试在 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
, 如果颜色列表被指定为选项 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/