How can I enlarge the legend box-size?
356 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Naveen
el 14 de Feb. de 2013
Comentada: Jan
el 19 de Jun. de 2024
Hi,
I am plotting two curves and defining their parameters using the legend. The code is as follows
hleg1 = legend ( ['$ n/n_c = $' num2str(den1)], ['$ n/n_c = $' num2str(den2)] );
set(hleg1,'Interpreter','latex')
set(gca,'fontsize',15)
My problem is that as soon as I change the font size to 15 the box around the legends doesn't expand automatically and I have legends of the curves going outside the legend box. Can anybody suggest something?
0 comentarios
Respuesta aceptada
Walter Roberson
el 14 de Feb. de 2013
legend() is implemented by creating a new axis, the handle of which is returned. You should be setting Interpreter and fontsize for the text entries rather than the axis.
[hleg1, hobj1] = legend( ...);
textobj = findobj(hobj1, 'type', 'text');
set(textobj, 'Interpreter', 'latex', 'fontsize', 15);
Then if you find you need to, set() the Position property of hleg1.
2 comentarios
Walter Roberson
el 15 de Feb. de 2013
You have a legend with two entries. The second value returned by legend() is the handles to the objects that went up to make the legend, including the text objects. The findobj() reduces the list down to just the text objects. The set() then affects all of those text objects. So already this code works for legends with multiple entries.
Más respuestas (2)
Azzi Abdelmalek
el 14 de Feb. de 2013
Editada: Azzi Abdelmalek
el 14 de Feb. de 2013
set(hleg1,'position',[0 0 0.2 0.2])
In general
set(hleg1,'position',[x0 y0 width height])
3 comentarios
Jan
el 19 de Jun. de 2024
Does this syntax still work in 2022b?
set(hleg1,'position',[x0 y0 width height])
I try to make the box twice as high to increase the spacing between legend entries (because one of them is multi-line).
set(hleg1,'Location', 'NorthEastOutside');
sz = get(hleg1, 'position');
sz(2) = sz(2)-sz(4); % Make the bottom lower
sz(4) = 2*sz(4); % Make the heigth larger
set(hleg1,'position', sz);
This does indeed resize the legend, but the axes switch to full width, which is unwanted. Adding
set(hleg1,'Location', 'NorthEastOutside');
again, after all this also resizes the legend back to its original tight version.
Max Riedel
el 6 de Abr. de 2016
A simple workaround is to put additional spacing after the text.
E.g., by using \quad:
hleg1 = legend ( ['$ n/n_c = $' num2str(den1) '$\quad$'], ['$ n/n_c = $' num2str(den2) '$\quad$'] );
0 comentarios
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!