彩色地图的绝对比例?

时间:2014-11-30 23:49:18

标签: matlab matlab-figure

我试图制作一个强度随时间变化的情节:

[X,Y] = meshgrid(-30:.1:30);
figure;
colormap(bone);


for t = 0:0.1:2*pi
    R = sqrt(X.^2 + Y.^2);
    Z = cos(t)*abs(besselj(2,R));
    surf(Z,'EdgeColor','None');
    view(90,90);
    axis([0 600 0 600 -0.5 0.5])
    pause(0.1);
end

我想从顶部看这个,这样随着Z值的变化,颜色会发生变化。问题在于,不是具有绝对标度(黑色= -0.5,白色= 0.5),色标是相对于最大值和最小值,使得颜色仅在标志翻转变化时改变。如何设置颜色图的绝对比例?

谢谢。

2 个答案:

答案 0 :(得分:1)

如果您只想要小于0的值为白色而对于大于0的值则为黑色,那么您可以这样做:

surf(Z,sign(Z),'EdgeColor','None');

使用C参数surf,告诉Matlab根据C而不是Z的值对图进行着色。 sign(Z)是一个矩阵,其中1为Z>0,0为Z=0,-1为Z<0

答案 1 :(得分:1)

您必须使用scaled颜色映射模式,并使用caxis命令设置缩放限制。

现在你当前代码的问题是你在循环的每次迭代中调用surf,基本上每次都会破坏当前的绘图并生成一个新的绘图。这会重置很多属性,包括caxis限制为auto。要解决这个问题,只需在循环之前创建一次绘图,然后在循环中只更改修改的属性(在这种情况下为Z值)。这样,图中所有其他内容都保持不变。

所以你的代码变成了:

%% // Prepare and initialize the surface plot
[X,Y] = meshgrid(-30:.1:30);
R = sqrt(X.^2 + Y.^2) ;         %// this doesn't need to be in the loop

Z = cos(0)*abs(besselj(2,R)) ;                  %// calculate initial value to create the surface
surfHandle = surf( Z , 'EdgeColor','None' ) ;   %// create a first surface, and save the handle to the surface object
colormap(bone);
colorbar                        %// this is optional, just to make sure the colorbar does not vary
caxis([-5 5 ] ) ;               %// this is what sets the color scaling to what you want
view(90,90);
axis([0 600 0 600 -0.5 0.5]) ;  %// this doesn't need to be in the loop anymore

%% // Modify and update the surface plot
for t = 0:pi/100:2*pi
    Z = cos(t)*abs(besselj(2,R));
    set( surfHandle , 'ZData' , Z )
    drawnow
    pause(0.01);
end

阅读coloring-mesh-and-surface-plots,了解有关表面如何着色的详细信息。