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

5 views (last 30 days)
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');

Answers (1)

Ahmet Cecen
Ahmet Cecen on 4 May 2015
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 Comments
A
A on 4 May 2015
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 on 5 May 2015
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;

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!