【问题标题】:Quiver 3d (python matplotlib): how to get same color for arrow shaft and arrow head? (Quiver bug?)Quiver 3d(python matplotlib):如何为箭头轴和箭头获得相同的颜色? (颤抖虫?)
【发布时间】:2020-05-04 16:24:16
【问题描述】:

下面的程序绘制了 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

输出:

【问题讨论】:

    标签: python python-3.x matplotlib


    【解决方案1】:

    我认为问题在于您没有指定箭头的颜色,因此 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 2015-10-18
      • 1970-01-01
      • 2010-12-14
      相关资源
      最近更新 更多