Can I manipulate the legend?

1 次查看(过去 30 天)
Coral Reyes
Coral Reyes 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