在Mayavi中将色彩图集中在0左右

时间:2015-10-01 15:39:23

标签: python mayavi color-mapping

我正在绘制点云并通过残差来着色。我希望色彩图保持居中于0,这样0错误就是白色。

我看到answers for matplotlib。玛雅维怎么样?

from mayavi import mlab
mlab.points3d(x, y, z, e, colormap='RdBu')

2 个答案:

答案 0 :(得分:1)

您可以使用vmin明确设置色彩映射的vmaxmlab.points3d。因此,您可以确保vmin = -vmax。像这样:

mylimit = 10
mlab.points3d(x, y, z, e, colormap='RdBu',vmin=-mylimit,vmax=mylimit)

或者,您可以使用以下内容自动设置限制:

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

答案 1 :(得分:1)

如果有人希望这样做但是为了使用颜色条的完整范围,这里是我为mayavi做出的解决方案(在here的帮助下),它可以拉伸颜色条使其成为中心是零:

#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表并修改每一次。