how to decrease the size of the legend in a figure
172 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ban zhihua
el 16 de Nov. de 2017
Comentada: Andre Zeug
el 11 de Jun. de 2019
The following code plots a figure. I want to remove the space within the legend. I spend one day to try to solve this problem. However, I failed. Would you like to help me.
x = 0:0.5:10;
figure; hold on;
plot(x,sin(x), 'Marker', 'o');
plot(x,cos(x), 'Marker', 's');
[leg, objs] = legend({'sin', 'cos'}, 'Location', 'SouthWest');
line_start_end = [0.01, 0.4];
line_text_step = 0.01;
% for each line, text object, adjust their position in legend
for i = 1:numel(objs)
if strcmp(get(objs(i), 'Type'), 'line')
% line object
if 2 == numel(get(objs(i), 'XData')) % line
set(objs(i), 'XData', line_start_end);
else % marker on line
set(objs(i), 'XData', sum(line_start_end)/2);
end
else
%text object
text_pos = get(objs(i), 'Position');
text_pos(1) = line_start_end(2) + line_text_step;
set(objs(i), 'Position', text_pos);
end
end
see the following result.

What I want is :

0 comentarios
Respuesta aceptada
ban zhihua
el 20 de Nov. de 2017
1 comentario
Andre Zeug
el 11 de Jun. de 2019
Hi, having 'problems' with the legends function @ R2019a.
As soon as I add second output parameter, 'legend' does not work (as expected).
x = 0:0.5:10;
figure; hold on;
for n=1:10
plot(x,sin(x+n/10),'DisplayName',sprintf('sin %2d',n));
end
[hleg, hobj] = legend;
set(hleg,'Location','best','NumColumns',3,'FontSize',7);
it is slightly different with:
[hleg, hobj] = legend('Location','best','NumColumns',3,'FontSize',7);
it works however fine with:
[hleg] = legend('Location','best','NumColumns',3,'FontSize',7);
Question: how to 'find'
hobj
in the latter case?
Más respuestas (1)
KSSV
el 16 de Nov. de 2017
plot(rand(4))
lgd = legend('One','Two','Three','Four');
lgd.FontSize = 24;
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!