【问题标题】:Mayavi points3d with different size and colors不同大小和颜色的 Mayavi points3d
【发布时间】:2014-04-10 18:57:08
【问题描述】:

是否可以在 mayavi 中单独指定每个点的大小和颜色?

那个 API 对我来说很麻烦。

points3d(x, y, z...)
points3d(x, y, z, s, ...)
points3d(x, y, z, f, ...)

x, y and z are numpy arrays, or lists, all of the same shape, giving the positions of the points.
If only 3 arrays x, y, z are given, all the points are drawn with the same size and color.
In addition, you can pass a fourth array s of the same shape as x, y, and z giving an associated scalar value for each point, or a function f(x, y, z) returning the scalar value. This scalar value can be used to modulate the color and the size of the points.

因此,在这种情况下,标量同时控制大小和颜色,并且无法将它们分开。我想要一种方法将大小指定为(N,1) 数组,并将颜色指定为另一个(N,1) 数组。

看起来很复杂?

【问题讨论】:

    标签: python data-visualization vtk mayavi


    【解决方案1】:

    每个 VTK 源都有一个标量和向量数据集。

    我在我的程序中使用的使颜色和大小不同的技巧是绕过 mayavi 源代码,直接在 VTK 源代码中,使用标量作为颜色,使用向量作为大小(它可能也适用于其他方式) .

    nodes = points3d(x,y,z)
    nodes.glyph.scale_mode = 'scale_by_vector'
    
    #this sets the vectors to be a 3x5000 vector showing some random scalars
    nodes.mlab_source.dataset.point_data.vectors = np.tile( np.random.random((5000,)), (3,1))
    
    nodes.mlab_source.dataset.point_data.scalars = np.random.random((5000,))
    

    您可能需要转置 5000x3 矢量数据或以其他方式移动矩阵维度。

    【讨论】:

    • 这张图片是来自 Crossley 等人的共激活矩阵?我试图用 Mayavi 可视化的相同图像。不小心我正在研究它,我发现你的 bctpy 工具箱非常有用。我只是想说声谢谢,我要在github上fork它。
    • 什么共激活矩阵?这张图片是来自我的可视化程序 CVU 的大脑网络,它自动生成了关于大小和颜色的网络统计信息(使用 bctpy 计算,这就是我创建 bctpy 的原因)。我很高兴人们在使用 bctpy。
    【解决方案2】:

    我同意 Mayavi 在此处提供的 API 令人不快。 Mayavi documentation 建议使用以下技巧(我稍微解释一下)来独立调整点的大小和颜色。

    pts = mayavi.mlab.quiver3d(x, y, z, sx, sy, sz, scalars=c, mode="sphere", scale_factor=f)
    pts.glyph.color_mode = "color_by_scalar"
    pts.glyph.glyph_source.glyph_source.center = [0,0,0]
    

    这会将x,y,z 点显示为球体,即使您正在调用mayavi.mlab.quiver3d。 Mayavi 将使用sx,sy,sz 向量的范数来确定点的大小,并将使用c 中的标量值来索引颜色映射。您可以选择提供一个恒定大小的比例因子,它将应用于所有点。

    这当然不是你写过的最能自我记录的代码,但它确实有效。

    【讨论】:

      【解决方案3】:

      我也同意 API 很丑。我只是用@aestrivex的想法做了一个简单而完整的例子:

      from mayavi.mlab import *
      import numpy as np
      
      K = 10
      xx = np.arange(0, K, 1)
      yy = np.arange(0, K, 1)
      
      x, y = np.meshgrid(xx, yy)
      x, y = x.flatten(), y.flatten()
      z = np.zeros(K*K)
      
      colors = 1.0 * (x + y)/(max(x)+max(y))
      
      nodes = points3d(x, y, z, scale_factor=0.5)
      nodes.glyph.scale_mode = 'scale_by_vector'
      
      nodes.mlab_source.dataset.point_data.scalars = colors
      
      show()
      

      产生:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多