在Mayavi中将颜色映射居中于0

3
我正在绘制一个点云,并通过残差误差进行着色。我希望色图保持在0的中心,以便0误差为白色。 我看到 matplotlib的答案。那么Mayavi呢?
from mayavi import mlab
mlab.points3d(x, y, z, e, colormap='RdBu')
2个回答

1

如果有人想要这样做,但希望使用整个色条的范围,那么我做了一个解决方案(在这里得到帮助),可以将mayavi的色条拉伸,使其中心位于零:

#Mayavi surface
s = mlab.surf(data)
#Get the lut table of the data
lut = s.module_manager.scalar_lut_manager.lut.table.asarray()

maxd = np.max(data)
mind = np.min(data)
#Data range
dran = maxd - mind

#Proportion of the data range at which the centred value lies
zdp = abs(mind / dran)    

#The +0.5's here are because floats are rounded down when converted to ints
#index equal portion of distance along colormap
cmzi = int(zdp * 255 + 0.5)
#linspace from zero to 128, with number of points matching portion to side of zero
topi = np.linspace(0, 127, cmzi) + 0.5
#and for other side
boti = np.linspace(128, 255, 255 - cmzi) + 0.5    

#convert these linspaces to ints and map the new lut from these    
shift_index = np.hstack([topi.astype(int), boti.astype(int)])
s.module_manager.scalar_lut_manager.lut.table = self.lut[shift_index]

#Force update of the figure now that we have changed the LUT
mlab.draw()

请注意,如果您希望对同一表面进行多次操作(即,如果您修改的是Mayavi标量而不是重新绘制图形),则需要记录初始LUT表并每次修改它。

1
您可以使用mlab.points3d显式设置颜色映射的vminvmax。所以,您只需要确保vmin = -vmax即可。类似于这样:
mylimit = 10
mlab.points3d(x, y, z, e, colormap='RdBu',vmin=-mylimit,vmax=mylimit)

或者,您可以使用类似以下的方式自动设置限制:

mylimit = max(abs(e.min()),abs(e.max()))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接