Can I manipulate the legend?

조회 수: 1 (최근 30일)
Coral Reyes
Coral Reyes 2015년 7월 13일
댓글: Star Strider 2015년 7월 13일
The following code will plot me 68 markers. Each marker will have a different color depending on the value of I. Is there a way I can get my legend to show all 5 colors ? For now it is just showing the marker for the first 5 plots, and I just need one per color. If you have any idea or other suggestions that can work as well please let me know.
%for ii=1:1:length(x)
if I(ii)==0;
plot(x(ii), y(ii),'bx','LineWidth',2,'MarkerSize',15)
hold on %attached flow
elseif I(ii)==1;
plot(x(ii), y(ii),'yx','LineWidth',2,'MarkerSize',15)
hold on %energized
elseif I(ii)==2;
plot(x(ii), y(ii),'mx','LineWidth',2,'MarkerSize',15)
hold on %highly energized
elseif I(ii)==3;
plot(x(ii), y(ii), 'rx','LineWidth',2,'MarkerSize',15)
hold on %seperated
else
plot(x(ii), y(ii), 'x','LineWidth',2,'MarkerSize',15)
hold on %incipient
end
%end

답변 (1개)

Star Strider
Star Strider 2015년 7월 13일
Try this:
L = 100;
I = randi([0 4], 1, L);
x = randn(1,10);
y = randn(1,10);
figure(1)
hold on
for ii=1:1:length(x)
if I(ii)==0;
hl0 = plot(x(ii), y(ii),'bx','LineWidth',2,'MarkerSize',15)
% hold on %attached flow
elseif I(ii)==1;
hl1 = plot(x(ii), y(ii),'yx','LineWidth',2,'MarkerSize',15)
% hold on %energized
elseif I(ii)==2;
hl2 = plot(x(ii), y(ii),'mx','LineWidth',2,'MarkerSize',15)
% hold on %highly energized
elseif I(ii)==3;
hl3 = plot(x(ii), y(ii), 'rx','LineWidth',2,'MarkerSize',15)
% hold on %seperated
else
hl4 = plot(x(ii), y(ii), 'x','LineWidth',2,'MarkerSize',15)
% hold on %incipient
end
end
hold off
legend([hl0, hl1, hl2, hl3, hl4], 'attached flow', 'energized', 'highly energized', 'seperated', 'incipient')
  댓글 수: 2
Coral Reyes
Coral Reyes 2015년 7월 13일
it didn't work . It says
Undefined function or variable 'hl1'
Star Strider
Star Strider 2015년 7월 13일
That simply means that there were no values of I(ii) that were equal to 1. It works if there are — I tested it to be sure.
I don’t have your data, so I can only simulate them and test your code against my simulations.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by