如何减少图中图例的大小?

时间:2017-11-16 05:49:18

标签: 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

2 个答案:

答案 0 :(得分:3)

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

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

resizeLegend(); 

给出:

output

答案 1 :(得分: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

结果

https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#aapt2