Create legend based on a if loop statement

4 visualizaciones (últimos 30 días)
Jeroen van Bemmel
Jeroen van Bemmel el 25 de Mayo de 2022
Comentada: Jeroen van Bemmel el 25 de Mayo de 2022
I have a for-loop and if-loop making a scatter plot, where a 3D coordinate is plotted with one of three possible colors based on the if loop condition. so the coordinate becomes either green, red or black. I want to make a legend denoting these three conditions, but my current code denotes three green dots instead of the three different colors.
Anybody knows how to fix this problem?
Thanks in advance
figure(1)
for j=1:16
for i=(1+((j-1)*61)):(30+((j-1)*61)) % inputting command (only gets desired nodes from data file)
if CPN_1(i) < -1 && CPN_1(i) > -2 % pressure between -2 and -1:color red
scatter3(x_1(i),y_1(i),z_1(i),200,'filled','red')
hold on
elseif CPN_1(i) < -2
scatter3(x_1(i),y_1(i),z_1(i),250,'filled','black') % pressure below -2, color black
hold on
else
scatter3(x_1(i),y_1(i),z_1(i),100,'s','filled','green') % pressure higher then -1, color green
hold on
end
end
end
xlabel('x [mm]')
ylabel('y [mm]')
zlabel('z [mm]')
title('Pressure distribution suction side')
legend('-C_{PN} > 2', '2> -C_{PN} > 1','-C_{PN} < 1') % current wrong legend command
hold off
  1 comentario
Jeroen van Bemmel
Jeroen van Bemmel el 25 de Mayo de 2022
for people wondering how i fixed the problem by the answer of Walter Roberson:
made the compution much faster as well!
figure(1)
count_green=1; %counters to construct the three subsets from position 1 to x
count_red=1;
count_black=1;
for j=1:16
for i=(1+((j-1)*61)):(30+((j-1)*61))
if CPN(i) < -1 && CPN(i) > -2 % stores all values which meet the condition in their corresponding subset
CPN_red(count_red)=CPN(i);
x_red(count_red)=x(i);
y_red(count_red)=y(i);
z_red(count_red)=z(i);
count_red=count_red+1;
elseif CPN(i) < -2
CPN_black(count_black)=CPN(i); % stores all values which meet the condition in their corresponding subset
x_black(count_black)=x(i);
y_black(count_black)=y(i);
z_black(count_black)=z(i);
count_black=count_black+1;
else
CPN_green(count_green)=CPN(i); % stores all values which meet the condition in their corresponding subset
x_green(count_green)=x(i);
y_green(count_green)=y(i);
z_green(count_green)=z(i);
count_green=count_green+1;
end
end
end
figure(1) % plot the three subsets in the same figure
scatter3(x_red,y_red,z_red,150,'filled','red')
hold on
scatter3(x_black,y_black,z_black,250,'filled','black')
hold on
scatter3(x_green,y_green,z_green,80,'filled','green')
legend('2>-C_{PN}>1','-C_{PN}>2','-C_{PN}<1') % legend is filled correctly now!

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Mayo de 2022
you are doing a separate scatter3 for each point. Each of those will result in a new plot object. Your legend call with three labels will attach the labels to the first three scatter3 objects.
Using separate scatter3 for each point is inefficient.
Your code appears to be processing the first 30 rows of a 61x16 array. You would be better off extracting a subset of the desired data, and then vectorizing the tests using logical indexing to create three different subsets that you scatter3() all of the subset, ending up with just 3 scatter3 calls.

Más respuestas (0)

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by