2014-03-07 50 views
11

mayavi可以單獨指定每個點的大小和顏色嗎?Mayavi points3d具有不同的尺寸和顏色

該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)陣列單獨..

似乎複雜?

回答

8

每個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矢量數據或以其他方式移動矩陣尺寸。

enter image description here

+0

此圖片是從克羅斯利等人的共激活矩陣?我試圖用Mayavi想象的相同圖像。無意中,我正在處理它,我發現你的bctpy工具箱非常有用。我只是想說謝謝,我會在github上分享它。 – linello

+0

什麼共激活矩陣?該圖像是我的可視化程序CVU中的一個大腦網絡,具有自動生成的大小和顏色的網絡統計數據(使用bctpy計算,這就是爲什麼我創建了bctpy)。 我很高興人們使用bctpy。 – aestrivex

5

我同意,那Mayavi的規定這裏的API是不愉快的。 Mayavi documentation建議以下hack(我已經稍微解釋了一些)來獨立調整點的大小和顏色。

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中的標量值將其索引到彩色圖中。您可以選擇提供恆定大小的比例因子,該因子將應用於所有點。

這當然不是你寫的最自我記錄的代碼,但它的工作原理。

0

我也同意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() 

主要生產:

enter image description here