【问题标题】:Matplotlib 3D Quiver plot makes the lines the right color but the arrow head the wrong colorMatplotlib 3D Quiver plot 使线条颜色正确,但箭头颜色错误
【发布时间】:2020-11-27 15:38:40
【问题描述】:

我正在尝试在 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()

【问题讨论】:

  • 你只有 3 支箭,为什么要列出 6 个箭筒?
  • 你检查过这个stackoverflow.com/questions/28420504/…吗?它谈到了着色
  • 这对我来说似乎是一个错误
  • quiver 中,如果将颜色列表指定为选项colors=..,则颜色循环对所有线段有效,包括箭头的所有部分。

标签: python matplotlib


【解决方案1】:

quiver 中,如果将颜色列表指定为选项colors=..,则颜色循环对所有线段有效,包括箭头的所有部分。

要获得 3 个不同颜色的箭头,您可以使用 3 个 quiver 语句,每个语句具有不同的颜色。

方法1

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_)

【讨论】:

  • 谢谢,我通过使用 cols = ['r', 'g', 'b', 'r', 'r', 'g', 'g', 'b' 解决了我的问题, 'b'] 现在可以正确循环颜色(假设只绘制了三条线)。
  • @TomMcLean 不错的尝试。请检查我的更新答案。
  • 谢谢,这是一个更好的方法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 2021-01-05
  • 1970-01-01
相关资源
最近更新 更多