【问题标题】:Colormap for quiver3d() in MayaviMayavi 中 quiver3d() 的颜色图
【发布时间】:2017-06-28 15:19:22
【问题描述】:

我正在尝试绘制一个矢量场,并且我正在使用 Mayavi2s quiver3d() 函数。我希望根据单位球体上的角度对箭头进行着色:

我可以这样做:

def color(theta, phi):
    while(phi < 0):
        phi += 2.0*np.pi
    h = phi / (2.0 * np.pi)
    s = 1
    v = 1.0 - 0.9999999*(theta / np.pi)
    print("h = {}, s = {}, v ={}".format(h,s,v))
    return hsv_to_rgb(h,s,v)

def plot_nice():
    x = np.array([1.,2.,3.])
    y = np.array([0.,0.,0.])
    z = y

    phi   = np.linspace(0,np.pi/2.0,3)
    theta = phi

    u = np.sin(theta) * np.cos(phi)
    v = np.sin(theta) * np.sin(phi)
    w = np.cos(theta)

    obj = quiver3d(x, y, z, u, v, w,
            line_width=3, colormap='hsv', 
            scale_factor=0.8, mode='arrow',resolution=25)

    for i in range(x.shape[0]):
        r,g,b = color(theta[i], phi[i])
        print("R: {}, G: {}, B: {}".format(r,g,b))
        obj = quiver3d(x[i], y[i], z[i], u[i], v[i], w[i],
                line_width=3, color=(r,g,b), colormap='hsv', 
                scale_factor=0.8, mode='arrow',resolution=25)
    return obj

figure(bgcolor=(1,1,1))
plot_nice()

但如果我有数百支箭,这会很慢。我可以以更快的方式做到这一点:

def plot_fast():
    x = np.array([1.,2.,3.])
    y = np.array([0.,0.,0.])
    z = y

    phi   = np.linspace(0,np.pi/2.0,3)
    theta = phi

    u = np.sin(theta) * np.cos(phi)
    v = np.sin(theta) * np.sin(phi)
    w = np.cos(theta)

    obj = quiver3d(x, y, z, u, v, w,
            line_width=3, colormap='hsv', 
            scale_factor=0.8, mode='arrow',resolution=25)
plot_fast()

但是我失去了颜色图:

如何在不必为每个箭头创建新图的情况下创建类似于上图的内容?

【问题讨论】:

    标签: python python-2.7 plot mayavi


    【解决方案1】:

    可以通过事后操纵情节对象来实现。

    quiver3d 调用中,添加参数scalars=np.mod(phi, 2*np.pi)(例如)和scale_mode='none'。 调用后,添加一行

    obj.glyph.color_mode = 'color_by_scalar'
    

    根据记忆,可能可以单独控制箭头的颜色和大小,但这有点复杂。

    【讨论】:

    • 如果我对你的战争理解正确,那么我只能编码一维信息,对吗?我想编码极角和方位角。例如。像这样:en.wikipedia.org/wiki/HSL_and_HSV
    • 您可以自定义长度或自定义颜色。颜色可以以您选择的任何方式取决于 phi 和 theta。 scalars 参数的长度必须与 x、y、z、u、v 和 w 相同,仅此而已。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    相关资源
    最近更新 更多