Marker and Line in legend in matlab plot

Hi, I am plotting the experimental data and fitted line using matlab plot. The legend command shows the markers (data points) and line (fit) as a separate legend entry. However, I am interested to obtain the marker and line in a single legend entry so that the total number of legend entries could be reduced from 8 to 4 in my case. Hence, I would like to ask your assistance which could be useful for my plot. Thank you.

 Accepted Answer

Create additional line objects that combine the attributes, such as
LH(1) = plot(nan, nan, '*-r');
L{1} = 'tadpoles';
LH(2) = plot(nan, nan, '^:g');
L{2} = 'cheese';
Then legend() those
legend(LH, L)

8 Comments

Hi Walter, Thank you for your response. However, I have the situation similar to Stevan as shown in the link for your kind reference http://se.mathworks.com/matlabcentral/answers/711#answer_1080. I also tried the accepted answer by Martijn but found some errors. Please let me know if you have any suggestions or comments.
Example:
x = 1 : 20;
y1 = sin(x);
plot(y1, 'r-');
hold on
plot(y1+randn(size(x)), 'r*');
y2 = tan(x.*(x-3).*(x-5));
plot(y2, 'b:');
plot(y2 .* rand(size(x)), 'b^');
LH(1) = plot(nan, nan, 'r-*');
L{1} = 'tadpoles';
LH(2) = plot(nan, nan, 'b:^');
L{2} = 'cheeses';
legend(LH, L);
hold off
Notice how the solid red line and the red * plot independently but there is a single red line with * in the legend. Likewise notice how the dotted blue line and the blue triangles plot independently but there is a single doted blue line with triangle in the legend.
Do not legend() the line handles that have the individual attributes. Instead, create some new lines that have the desired pair of attributes and legend() those new lines. Those new lines do not show up in the drawing because you set their data to nan and nan is never drawn on screen -- but not being drawn on screen does not mean they cannot have legend() applied to them.
Thank you Walter.
What is the significance of writing nan as the 1st and 2nd argument in plot? (it only works if I type that in so I'm curious to understand what it stands for)
@Andrew, nan signifies "not-a-number". The plot() with these arguments, together with the line specification creates an empty line with only a line specification. The legend then point to this "empty" line object and not to the actual visible line object.
Hi Dear Walter When I applied this code I have this error "Unable to assignment because brace indexing is not supported for variables of this type"
Displaying appropriate markers in legend when plotting 2D arrays against 1D array.
% create array data
D1=rand(500,12);
D2=rand(500*0.15,12);
t=[7:18];
% plot array data where each columm correspond to one element of the x vector
figure(1),clf
subplot(121)
p1=plot(t,D1,'g.');hold on
p2=plot(t,D2,'+k');
legend([p1(1),p2(1)],'100% cars EV','15% cars EV');
% to be able to display each marker for D1 and D2 need to use plot handle
% for one of the data plotted in each plot => (1)
subplot(122)
plot(t,D1,'g.');hold on
plot(t,D2,'+k');
legend('100% cars EV','15% cars EV');
% without using plot handle in the legend command you can only see the same
% marker
Yes, that is expected. When you call legend() and do not pass in handles of graphics objects, then it looks to see how many legend entries you supplied (two in this case) and it finds the first that-many (so, first 2 in this case) graphics objects and creates legends for those. Your plot(t,D1,'g.') is creating multiple graphics objects because D1 is a 2D array, so the first two graphic handles are both associated with D1.
If you want to create one legend entry for each line in D1 then you can do that:
% create array data
D1=rand(500,12);
D2=rand(500*0.15,12);
t=[7:18];
% plot array data where each columm correspond to one element of the x vector
figure(1),clf
subplot(121)
p1=plot(t,D1,'g.');hold on
p2=plot(t,D2,'+k');
legend([p1(1),p2(1)],'100% cars EV','15% cars EV');
% to be able to display each marker for D1 and D2 need to use plot handle
% for one of the data plotted in each plot => (1)
subplot(122)
p3 = plot(t,D1,'g.');hold on
p4 = plot(t,D2,'+k');
L3 = "100% cars EV #" + (1:length(p3)).';
set(p3, {'DisplayName'}, cellstr(L3));
L4 = "15% cars EV #" + (1:length(p4)).';
set(p4, {'DisplayName'}, cellstr(L4));
legend([p3; p4], 'location', 'eastoutside', 'NumColumns', 4)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!