Matlab中使用不同的颜色方案绘制surf图

5
我有一张地形图,想在上面展示一些数据。如下图所示: enter image description here 右侧被白色圈起来的区域是一个单独的surf函数,与绘图的其余部分不同。我想要做的是更改颜色方案。外部应该是灰度,内部应该是一种单一的颜色,基于我与绘图分开的值。目前我已经尝试了colormap(gray)函数,但是那会改变整个绘图。
我可以接受关于不同绘图样式的建议,例如使用plot3而不是surf。因此,我用于制作这两个surf的数据是两个x、y、z点列表。
如果可能的话,我也希望显示一个表示圆形区域颜色的颜色条(由我根据外部值设置)。
有人知道一个好方法吗?
谢谢。
编辑:
我想要做的是这样的: enter image description here 图片顶部不应有深蓝色。图像将不断更新,有更多“蓝点”,颜色应基于外部值更改,并且如果它们重叠,最好与以前的点合并颜色。
2个回答

7

如果您想将圆圈区域设置为单一颜色,可以设置其FaceColor属性。例如:

%# make some test data
[xx,yy]=ndgrid(-5:0.1:5,-5:0.1:5);
zz = exp(-xx.^2/2+-yy.^2/2);
zz1 = zz;
zz1(zz1>0.5)=NaN;
zz2 = zz;
zz2(zz2<0.5)=NaN;

%# plot first surface, set colormap
surf(zz1)
colormap('gray')

%# stretch colormap to [0 0.5]
caxis([0 0.5])

%# plot the second surface in red
hold on
surf(zz2,'faceColor','r')

enter image description here

编辑

如果您想为表面的部分设置不同的颜色图,您需要将表面的'CData'属性设置为颜色映射中的索引。 如果要在色条中仅显示单个颜色映射,则可以利用色条只是另一个绘图的事实,这意味着您可以仅显示其一部分并更改标签。

%# make some more test data
[xx,yy]=ndgrid(-5:0.1:5,-5:0.1:5);
zz = exp(-xx.^2/2+-yy.^2/2);
zz1 = zz(1:50,:);
zz2 = zz(52:end,:);
xx1 = xx(1:50,:);xx2=xx(52:end,:);
yy1 = yy(1:50,:);yy2=yy(52:end,:);

%# create multi-colormap, set it to figure
figure
cmap = [gray(128);copper(128)];
colormap(cmap)

%# plot surfaces, setting the cdata property to indices 1-128 and 129-256, 
%# respectively, in order to access the different halves of the colormap
surf(xx1,yy1,zz1,'cdata',round(127*(zz1-min(zz1(:))/(max(zz1(:))-min(zz1(:)))))+1,'cdatamapping','direct')
hold on
surf(xx2,yy2,zz2,'cdata',round(127*(zz2-min(zz2(:))/(max(zz2(:))-min(zz2(:)))))+129,'cdatamapping','direct')

%# find the handle to the colorbar
%# alteratively: cbarH = findall(gcf,'tag','Colorbar')
cbarH = colorbar;

%# set limits and ticks/labels
ylim(cbarH,[129 255])
set(cbarH,'ytick',[129 192 255],'yticklabel',[0 0.5 1])

enter image description here


@Ben:看起来我可能误解了你的问题——你似乎想要不同的颜色映射用于表面的不同部分。我已经添加了一个如何实现的示例。 - Jonas
谢谢,我正在尝试解决的问题又变得更加复杂了。我想为大部分地图设置一个颜色方案(即灰度),但中间部分应该是单一颜色,我计划继续添加这些具有不同纯色的表面。有关如何实现此目标的任何想法吗?(颜色代表不同的数据集,我正在尝试通过使用一系列颜色来表示它在3D表面上进行映射,这些数据将从0到3,并且是相当连续的。) - Fantastic Mr Fox
@Ben: 我不确定我理解你的意思。什么是“中间部分”?那是表面的另一部分吗?你想让这个带子在固定高度上绕行吗?你所说的单色是什么意思?只是红色,或绿色,或者是一个颜色映射,从黑色->绿色而不是从黑色->白色?如果你能用Photoshop举出个例子会更有帮助。 - Jonas

2

您是否看到过MATLAB技术支持的这篇文章?

http://www.mathworks.com/support/solutions/en/data/1-GNRWEH/index.html

您可以编辑colorbar属性。

g = colorbar;
get(g)

例如,
% Define a colormap that uses the cool colormap and 
% the gray colormap and assign it as the Figure's colormap.
colormap([cool(64);gray(64)])


% Generate some surface data.
[X,Y,Z] = peaks(30);


% Produce the two surface plots.
h(1) = surf(X,Y,Z);
hold on
h(2) = pcolor(X,Y,Z);
hold off


% Move the pcolor to Z = -10.
% The 0*Z is in the statement below to insure that the size
% of the ZData does not change.
set(h(2),'ZData',-10 + 0*Z)
set(h(2),'FaceColor','interp','EdgeColor','interp')
view(3)


% Scale the CData (Color Data) of each plot so that the 
% plots have contiguous, nonoverlapping values. The range 
% of each CData should be equal. Here the CDatas are mapped 
% to integer values so that they are easier to manage; 
% however, this is not necessary.


% Initially, both CDatas are equal to Z.
m = 64; % 64-elements is each colormap


cmin = min(Z(:));
cmax = max(Z(:));
% CData for surface
C1 = min(m,round((m-1)*(Z-cmin)/(cmax-cmin))+1); 
% CData for pcolor
C2 = 64+C1;


% Update the CDatas for each object.
set(h(1),'CData',C1);
set(h(2),'CData',C2);


% Change the CLim property of axes so that it spans the 
% CDatas of both objects.
caxis([min(C1(:)) max(C2(:))])

% I added these two lines
g = colorbar
set(g,'YLim',[1 60])

最后两行是我自己写的。其余内容来自MATLAB技术支持链接。它将为您提供仅使用一个颜色映射的色条。如果您想要灰色颜色映射的一半,则需要执行set(g,'YLim',[64 128])

enter image description here


嘿,你能想到一种方法来显示屏幕上的某个部分的颜色条吗? - Fantastic Mr Fox

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