【问题标题】:Assign colors to each points in points3d using mayavi python使用 mayavi python 为 points3d 中的每个点分配颜色
【发布时间】:2018-07-24 08:18:41
【问题描述】:

我对 3D 空间有一些看法。每个点都有一个颜色,使用以下公式计算-

// pack r/g/b into rgb
uint8_t r = 255, g = 0, b = 0;    // Example: Red color
uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b);

如上所示,RGB 颜色被打包成一个值。我正在尝试使用 mayavi python 可视化这些点。请看下面的代码sn-p-

from mayavi.mlab import *
import numpy as np

N = 10
# generate random points and colors (just for debugging)
(x, y, z) = np.random.random((3, N))
colors = np.random.random(N)

nodes = points3d(x, y, z, scale_factor=0.1)
nodes.glyph.scale_mode = 'scale_by_vector'
nodes.mlab_source.dataset.point_data.scalars = colors

show()

上面的代码使用随机颜色,它显示以下输出-

但是,我想指定颜色而不是使用随机值。请注意,每个点都有颜色。在这篇文章中,为了方便起见,我使用以下函数生成相同颜色的点-

def pack_rgb(r, g, b):
    rgb = (r<<16) + (g<<8) + b
    return rgb

colors = [pack_rgb(0, 255, 0) for _ in range(N)]

这会生成红色点,而不是如下所示的绿色点-

这里发生了什么? 我的目标是在 mayavi python 中可视化彩色点,其中每个点都有 RGB 颜色。

【问题讨论】:

  • 你搞清楚了吗?我也想做同样的事情,但不知道怎么做。
  • @physicsmajor:是的,我添加了我的答案。如果对您有用,请查看并接受答案!

标签: python data-visualization mayavi mayavi.mlab


【解决方案1】:

使用 Mayavi 可以轻松可视化 3D 空间中的点。可以为每个点单独分配 RGB 颜色。我们还可以为每个点设置一个比例因子。我从 Mayavi 社区找到了以下解决方案,我想在这里分享它-

from mayavi import mlab
import numpy as np

n = 100 # number of points
x, y, z = np.random.random((3, n))
rgba = np.random.randint(0, 256, size=(n, 4), dtype=np.uint8)
rgba[:, -1] = 255 # no transparency
pts = mlab.pipeline.scalar_scatter(x, y, z) # plot the points
pts.add_attribute(rgba, 'colors') # assign the colors to each point
pts.data.point_data.set_active_scalars('colors')
g = mlab.pipeline.glyph(pts)
g.glyph.glyph.scale_factor = 0.1 # set scaling for all the points
g.glyph.scale_mode = 'data_scaling_off' # make all the points same size

mlab.show()

请注意,以上代码需要 Mayavi 4.6.1 或更高版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 2012-12-26
    • 1970-01-01
    • 2021-11-27
    • 2014-12-23
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多