adding legend for multiple surface in same graph

Hi,
I am relatively new to Matlab. I have a figure (attached) which has two surfaces . I want to create legend for each surface. Any idea/suggestion how I can create legend for each surface? I have the attached the figure.
This is my code to create this graph
close all
clear variables
clc
test = xlsread('Matlabinput.xlsx', 'c2:f20');
x=test(:,1);
y=test(:,2);
z=test(:,3);
v=test(:,4);
xlin = linspace(min(x),max(x),13);
ylin = linspace(min(y),max(y),13);
[X,Y] = meshgrid(xlin,ylin);
f = scatteredInterpolant(x,y,z);
f2 = scatteredInterpolant(x,y,v);
Z = f(X,Y);
Z2 = f2(X,Y);
figure
mesh(X,Y,Z); %interpolated
axis tight ;
hold on;
surf( X, Y, Z, 'EdgeColor', 'black', 'FaceColor', [255,100,0]/255, 'FaceAlpha', .5 );
plot3(x,y,z,'.' );
axis tight;
mesh(X,Y,Z2) %interpolated
axis tight;
surf( X, Y, Z2, 'EdgeColor', 'black', 'FaceColor', [1,255,200]/255, 'FaceAlpha', .9);
plot3(x,y,v,'.' ) %nonuniform
%hold on
xlabel('h')
ylabel('s')
zlabel('tc')
alpha(0.5) %transparent
Thanks in advance.
Best,
Roni

 Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Sept. de 2015
Side note: you do not need surf() and then plot3(). You can use
surf( X, Y, Z, 'EdgeColor', 'black', 'FaceColor', [255,100,0]/255, 'FaceAlpha', .5, 'Marker', '.' );
To legend appropriately, you would use
h1 = surf( X, Y, Z, 'EdgeColor', 'black', 'FaceColor', [255,100,0]/255, 'FaceAlpha', .5, 'Marker', '.' );
and later
h2 = surf( X, Y, Z2, 'EdgeColor', 'black', 'FaceColor', [1,255,200]/255, 'FaceAlpha', .9, 'Marker', '.');
and then
legend([h1, h2], {'First Surface', 'Second Surface'});

3 comentarios

Roni Mohammad
Roni Mohammad el 22 de Sept. de 2015
Thanks Walter.
Kien Pham
Kien Pham el 14 de Nov. de 2018
Hi Walter, do you know of a way to turn off the facecolors altogether for the legend of multuple surface plots?
I set my FaceColor to 'interp,' and do not want a color to represent an interpolated range. I prefer to set the FaceColor of the boxes to a neutral white if possible. Let me know. Thanks.
In cases where you want aa legend that does not match the automatic legend rules, it is often easier to create some dummy invisible graphics objects that have the properties that you would like to have show up, and legend those instead . Sometimes there are ways to get the desired outputs by changing properties after calling legend but it is not clear that it is worth the fight at times .

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Object Properties en Centro de ayuda y File Exchange.

Preguntada:

el 21 de Sept. de 2015

Comentada:

el 14 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by