Combining legend data
27 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Steven White
el 2 de Feb. de 2011
Comentada: Jeen Doe
el 6 de Mayo de 2017
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?
0 comentarios
Respuesta aceptada
Martijn
el 2 de Feb. de 2011
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 comentarios
Martijn
el 6 de Abr. de 2016
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
el 6 de Mayo de 2017
In 2017a at least, the last line should be
objects(2).Children.LineStyle = '-';
Más respuestas (1)
Walter Roberson
el 2 de Feb. de 2011
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?
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!