【问题标题】:How to decrease the size of the legend in a figure?如何减小图中图例的大小?
【发布时间】:2018-04-29 13:40:29
【问题描述】:

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

查看以下结果:

我想要的是:

【问题讨论】:

  • 你也可以关掉盒子:lg.Box = 'off';

标签: matlab matlab-figure legend legend-properties


【解决方案1】:

正如@Sardar Usama 所建议的,我将我的解决方案放在这里。希望对你有用。

第一个答案很好。不过,我最终还是使用下面的代码来完成这个任务。

close all

x = 0:0.5:10;
figure; hold on;
ph(1) = plot(x,sin(x), 'Marker', 'o');
ph(2) = plot(x,cos(x), 'Marker', 's');

ax = gca;
ax.Box = 'on';

bx = axes('position', [ax.Position(1:2), 0.3, 0.4], 'Box', 'on', 'XTick', [], 'YTick', []);%, 'Color', [0.8549,0.7020,1.0000]);
cx = axes('position', [ax.Position(1:2), 0.3, 0.4], 'Box', 'on', 'XTick', [], 'YTick', []);%, 'Color', [0.8549,0.5020,1.0000]);

[legb, objsb] = legend(bx, ph, {'sin', 'cos'}, 'Location', 'SouthWest');
[legc, objsc] = legend(cx, ph, {'sin', 'cos'}, 'Location', 'SouthWest');

line_start_end = [0.01, 0.3];
line_text_step = 0.05;

legendshrink(ax.Position(1:2), legb, objsb, bx, line_start_end, line_text_step);
legendshrink(ax.Position(1:2), legc, objsc, cx, line_start_end, line_text_step);

% you need only to adjust cx' position.
cx.Position(1:2) = [ax.Position(1)+ax.Position(3)-cx.Position(3), ax.Position(2)];
legc.Position(1:2) = cx.Position(1:2);

其中legendshrink 定义为:

function legendshrink(leg_pos_xy, leg, objs, bx, line_start_end, line_text_step)

% leg_post_xy = ax.Position(1:2);
% [leg, objs] = legend(bx, line_handles, text_cell);
% 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

% get minimum possible width and height
legend_width_no_right = 0;
for i = 1:numel(objs)
  % legend margin left
  if strcmp(get(objs(i), 'Type'), 'line')
    if numel(get(objs(i), 'XData')) == 2
      leg_margin_x = get(objs(i), 'XData');
      leg_margin_x = leg_margin_x(1)*leg.Position(3);
    end
  else
    cur_right = get(objs(i), 'Extent');
    cur_right = (cur_right(1)+cur_right(3))*leg.Position(3);
    if cur_right > legend_width_no_right
      legend_width_no_right = cur_right;
    end
  end
end
legend_width  = legend_width_no_right + leg_margin_x;
legend_height = leg.Position(4);

bx.Position = [leg_pos_xy, legend_width, legend_height];
leg.Position(1:2) = bx.Position(1:2);
leg.Box = 'off';

end

结果

【讨论】:

    【解决方案2】:

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

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

    resizeLegend(); 
    

    给出:

    【讨论】:

      猜你喜欢
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2014-01-02
      • 2020-08-27
      • 1970-01-01
      • 2013-12-15
      • 2014-10-30
      相关资源
      最近更新 更多