gpt4 book ai didi

matlab - 如何减小图中图例的大小?

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:23 24 4
gpt4 key购买 nike

以下代码绘制了一个图形。该代码应适用于 Matlab >= R2014b。我想删除图例中的空格。如何做到这一点?

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

看到如下结果:

result

我想要的是:

required

最佳答案

有一个名为 resizeLegend by David J. Mack 的提交在文件交换上正是这样做的。

你可以只用这一行替换你的循环:

resizeLegend(); 

给出:

output

关于matlab - 如何减小图中图例的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47322514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com