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

标签 matlab matlab-figure legend legend-properties

以下代码绘制了一个图形。该代码应适用于 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/

相关文章:

matlab - 并集上的交集,但用 MATLAB 中的最小面积替换并集

linux - 是否可以使用 Eclipse 调试 mex 代码?

matlab - 如何使神经网络训练图表具有对数纵轴?

matlab - 在Matlab中生成动画(AVI文件)而不显示图形

dictionary - 传单通过图层选择添加/删除图例

r - 将 abline 的图例添加到 ggplot geom_point

matlab - 如何在 Matlab 中使用 plot3 限制到某个区域?

matlab - 在 MATLAB 中计算向量元素之间的最大距离

matlab - 将 MATLAB 图保存为质量为 300 DPI、居中的 PDF

r - 在 map 上独立移动 2 个图例 ggplot2