How can I modify the length of the lines in a legend?
Mostrar comentarios más antiguos
How can I reduce the length of the lines shown in the legend of a MATLAB plot?
Respuesta aceptada
Más respuestas (2)
Brandon Ballard
el 17 de Jul. de 2021
16 votos
Just to make it easier for people to find I have copied the approach listed in the comments which works for versions of Matlab R2018b onwards (I am currently using Matlab 2021a):
The simplest way to do it is to use: -
leg = legend('Plot1','Plot2',...);
leg.ItemTokenSize = [x1,x2];
By default x1=30 and x2=18 so put larger or smaller numbers as x1,x2 to increase or decrease the legend line size.
I can only modulate the length, not the width by using" leg = legend('Plot1','Plot2',...);
leg.ItemTokenSize = [x1,x2];"
Using multiple outputs from the legend command is discouraged and will start warning in R2024b. Also starting in R2024b, you can leverage the legend's IconColumnWidth property to adjust the width allocated to the icon of the legend.
p=plot(1:10);
l=legend(p);
l.IconColumnWidth = 10;
In cases when all the icons are markers, the value of IconColumnWidth will be adjusted to be narrow automatically. In all other cases, it will have its old default value which can still be adjusted.
scatter(rand(3,10), rand(3,10), 'filled');
l = legend;
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





