Group lines by color in the legend of a 3D plot

2 views (last 30 days)
Hi guys,
I'm struggling with the display of the legend in a 3D plot. I have a 3D plot of lines with some similar properties (color, line width, etc.) and what I basically want is to display the legend based on color only.
The thing is that I'm not familiar with the "set", "get" and "gca" functions so I'm completely lost when it comes to "parent" and "children" relations ... I've never understood what this gca function does or what is a handle, so I'm sure this is where the problem comes from.
1) When I do this, I have the correct display of colors and line width I want, but the legend is not correct. The legend shows each and every line that has been plotted while I wanted to group them by color.
for ind=1:ind_max
set(gca,'ColorOrder',colorOrder)
plot3(X1(:,ind),Y1(:,ind),Z1(:,ind))
hold on
plot3(X2(:,ind),Y2(:,ind),Z2(:,ind),'LineWidth',3)
plot3(X3(:,ind),Y3(:,ind),Z3(:,ind),'LineWidth',5)
end
legend
2) Looking in the manual, and some forum answer, I found something about the 'NextPlot' attribute. I tried to give it a shot but it was even worse. When I do this, the plot is not a 3D plot but a 2D plot. How come ?
for ind=1:ind_max
set(gca,'ColorOrder',colorOrder)
set(gca,'NextPlot','replacechildren')
plot3(X1(:,ind),Y1(:,ind),Z1(:,ind))
hold on
plot3(X2(:,ind),Y2(:,ind),Z2(:,ind),'LineWidth',3)
plot3(X3(:,ind),Y3(:,ind),Z3(:,ind),'LineWidth',5)
end
legend
3) When I do this, the plot is 3D plot but it the previous lines are erased at each iteration of the "for" loop
for ind=1:ind_max
set(gca,'ColorOrder',colorOrder)
plot3(X1(:,ind),Y1(:,ind),Z1(:,ind))
hold on
plot3(X2(:,ind),Y2(:,ind),Z2(:,ind),'LineWidth',3)
plot3(X3(:,ind),Y3(:,ind),Z3(:,ind),'LineWidth',5)
set(gca,'NextPlot','replacechildren')
end
legend
What am I doing wrong ? Thanks a lot.
Cheers,

Answers (0)

Community Treasure Hunt

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

Start Hunting!