Can I manipulate the legend?

1 view (last 30 days)
Coral Reyes
Coral Reyes on 13 Jul 2015
Commented: Star Strider on 13 Jul 2015
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

Answers (1)

Star Strider
Star Strider on 13 Jul 2015
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 Comments
Coral Reyes
Coral Reyes on 13 Jul 2015
it didn't work . It says
Undefined function or variable 'hl1'
Star Strider
Star Strider on 13 Jul 2015
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!