箭袋3d(python matplotlib):如何获得箭头杆和箭头的相同颜色? (颤抖的虫子?)

时间:2020-05-04 16:24:16

标签: python python-3.x matplotlib

下面的程序绘制3d箭头。我根据高度(z坐标)设置箭头的颜色。

工作原理:箭杆的颜色正确

问题:箭头(整个或部分)具有不同的颜色。丑。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import matplotlib.cm as cm

n = 5
n3 = n*n*n
u, v, w = np.arange(n3), np.arange(n3), np.arange(n3)  # Positions
x, y, z = np.arange(n3), np.arange(n3), np.arange(n3)  # Vector components
colors = [(0, 0, 255)]*n3
mid = n/2
for i in range(n):
    for j in range(n):
        for k in range(n):
            ii = i*n*n + j*n + k
            u[ii], v[ii], w[ii] = -(j-mid), i-mid, 0   # Whorl
            x[ii], y[ii], z[ii] = i, j, k
            colors[ii] = (1, 0, 0)
            if abs(k-(n-1)/2) < 1.1: colors[ii] = (0, 1, 0)
            if abs(k-(n-1)/2) < 0.1: colors[ii] = (0, 0, 1)

figure = plt.figure()
axes = figure.gca(projection='3d')  # gca = get/create current axes

q = axes.quiver(x, y, z, u, v, w, color=colors, length = 0.5, normalize=True)
plt.show()  # Finally display the plot

输出: Output from the code above

2 个答案:

答案 0 :(得分:0)

您会看到here,这是一个悬而未决的问题。

建议的解决方法是:

arrow_length_ratio=0.1

希望有帮助!

答案 1 :(得分:0)

我认为问题在于您没有指定箭头的颜色,所以 matplotlib 循环遍历您的 colors 变量来为箭头着色。

在 for 循环之后,使用下面的代码来修复箭头的颜色。每个向量由 3 个元素组成:向量主体的一条线加上箭头的两条线。因此,如果您有 n 向量,则需要提供 n*3 颜色以正确指定颜色。

假设您正在绘制 2 个向量。以下是矢量元素的颜色在颜色数组中的排序方式:line1col、line2col、l1arrow1col、l1arrow2col、l2arrow1col、l2arrow2col、l3arrow1col、l3arrow2col。

# fix arrowhead colors
cs = colors[:]
for c in colors:
    cs.append(c)
    cs.append(c)

figure = plt.figure(figsize=(13, 13))
axes = figure.gca(projection="3d")  # gca = get/create current axes
q = axes.quiver(x, y, z, u, v, w, color=cs, length=0.5, normalize=True)
plt.show()  # Finally display the plot

这是一个包含 3 个向量的简单示例,说明如何为向量着色。

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.quiver(
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [-3, 0, 0],
    [-4, 4, -1],
    [-2, 2, 1],
    color=plt.cm.viridis(
        [200, 50, 100, 200, 200, 50, 50, 100, 100]
    ),  # line1, line2, line3, l1arrow1, l1arrow2, l2arrow1, l2arrow2, l3arrow1, l3arrow2
    arrow_length_ratio=0.3,
)
ax.set(ylim=(-5, 5), xlim=(-5, 5), zlim=(-5, 5))
plt.show()
相关问题