how to decrease the size of the legend in a figure
542 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
ban zhihua
on 20 Nov 2017
1 Comment
Andre Zeug
on 11 Jun 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?
More Answers (1)
KSSV
on 16 Nov 2017
plot(rand(4))
lgd = legend('One','Two','Three','Four');
lgd.FontSize = 24;
0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!