Matlab Surface: Assign different color for specific portions of the graph.

2 次查看(过去 30 天)
Hi guys,
Is it possible to color different areas of ONE surface? and also have a legend?
Can I ask that anything > (25,25) be blue and less be red in the following example? Thanks!
x = [0:50];
y = [0:50];
Test1 = @(x,y) x.^2+y.^2;
[X1,Y1] = meshgrid(x,y);
Z1 = Test1(X1,Y1);
s1 = surf(X1,Y1,Z1,'LineStyle','none');

回答(1 个)

Ahmet Cecen
Ahmet Cecen 2015-5-4
How about:
x = [0:50];
y = [0:50];
Test1 = @(x,y) x.^2+y.^2;
[X1,Y1] = meshgrid(x,y);
Z1 = Test1(X1,Y1);
s1 = surf(X1,Y1,Z1,'LineStyle','none');
s1.FaceColor='blue';
hold on;
x = [0:25];
y = [0:25];
Test1 = @(x,y) x.^2+y.^2;
[X1,Y1] = meshgrid(x,y);
Z1 = Test1(X1,Y1);
s1 = surf(X1,Y1,Z1,'LineStyle','none');
s1.FaceColor='red';
  4 个评论
A
A 2015-5-4
Hi Ahmet,
Thanks very much. This is almost there. When I put this into my more complex formula, it gets a little confusing. Basically, when you graph this, is there a way to tell Matlab not to graph the green bit where Z = 0?
x = [0:10];
y = [0:10];
Eq1 = @(x,y)(x+y);
Eq2 = @(x,y)(2.*x+y);
BigEq = @(x,y) [Eq1(x,y).*(Eq2(x,y)>5)];
[X1,Y1] = meshgrid(x,y);
Z1 = BigEq(X1,Y1);
s1 = surf(X1,Y1,Z1);
set(s1,'FaceColor','green');
hold on;
x = [5:10];
y = [5:10];
BigEq = @(x,y) [Eq1(x,y).*(Eq2(x,y)>5)];
[X1,Y1] = meshgrid(x,y);
Z1 = BigEq(X1,Y1);
s1 = surf(X1,Y1,Z1);
set(s1,'FaceColor','red');
hold off;
Ahmet Cecen
Ahmet Cecen 2015-5-5
x = [0:10];
y = [0:10];
Eq1 = @(x,y)(x+y);
Eq2 = @(x,y)(2.*x+y);
BigEq = @(x,y) [Eq1(x,y).*(Eq2(x,y)>5)];
[X1,Y1] = meshgrid(x,y);
Z1 = BigEq(X1,Y1);
Z1(Z1==0)=NaN; % Get rid of zeros.
Z1(7:10,7:10)=NaN; % Get rid of the green surface where red will come, to resolve clipping.
s1 = surf(X1,Y1,Z1);
set(s1,'FaceColor','green');
hold on;
x = [5:10];
y = [5:10];
BigEq = @(x,y) [Eq1(x,y).*(Eq2(x,y)>5)];
[X1,Y1] = meshgrid(x,y);
Z1 = BigEq(X1,Y1);
s1 = surf(X1,Y1,Z1);
set(s1,'FaceColor','red');
hold off;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by