how to decrease the size of the legend in a figure
조회 수: 173 (최근 30일)
이전 댓글 표시
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
채택된 답변
ban zhihua
2017년 11월 20일
댓글 수: 1
Andre Zeug
2019년 6월 11일
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?
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!