What I would like to do is combine two legend items together. Suppose I have some like the following:
plot(4.5+rand(1,50),'o')
hold on
plot(5*ones(1,50))
legend('data','fit')
But instead of having 2 entries in the legend (1 circles, 1 line) I have only 1 entry (line with circle in the middle).
Does anybody know how to do this?

 채택된 답변

Martijn
Martijn 2011년 2월 2일

1 개 추천

What you could do here is first change your code a little to obtain handles to the plot:
p1= plot(4.5+rand(1,50),'o');
hold on
p2 = plot(5*ones(1,50));
Then only add a legend to the second plot (again make sure you obtain a handle):
l = legend(p2,'MyLegend');
Then modify the Marker used in this legend:
c = get(l,'Children');
set(c(1),'Marker','o')

댓글 수: 4

Steven White
Steven White 2011년 8월 17일
Perfect! This does exactly what I want it to do.
Many thanks,
Steven.
Yogesh T L
Yogesh T L 2016년 4월 6일
I have experimental data and fitted model with markers and lines respectively. So, I also have a similar situation like Stevan and tried to apply the above code to combine the markers and line in the legend but it doesn't work for me. So, please let me know if you have any suggestions.
Martijn
Martijn 2016년 4월 6일
The code above is for MATLAB releases before R2014b. For MATLAB release R2014b and newer with the new graphics system, this would become:
p1= plot(4.5+rand(1,50),'ob');
hold on
p2 = plot(5*ones(1,50),'b');
[~,objects] = legend('MyLegend');
objects(2).LineStyle = '-';
Jeen Doe
Jeen Doe 2017년 5월 6일
In 2017a at least, the last line should be
objects(2).Children.LineStyle = '-';

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 2월 2일

0 개 추천

When I try in 2008b Linux 64, I get two legend entries, as I expect.
Are you finding that you only get one legend entry, or are you trying to get only one legend entry? If you are trying to get only one legend entry, what are you hoping that it would look like considering you have two labels and two markers?

댓글 수: 1

Steven White
Steven White 2011년 8월 17일
I would only like one entry. The idea here is to represent the circle and the line as the same thing. For example, this could be data (circles) and fitted lines (lines). Hence, if I am plotting multiple datasets and fitted lines, 4 of each say, then I only have 4 legend entries, rather than 8.

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

카테고리

태그

질문:

2011년 2월 2일

댓글:

2017년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by