2013-06-30 247 views
3

在matlab中使用图例命令时,如何减少图例符号及其对应标签之间的水平距离?Matlab:减少图例中符号和标签之间的间距

示例代码:

Line1=plot(x1,y1,'s'); 
Line2=plot(x2,y2,'o'); 
Line3=plot(x3,y3,'^'); 
Leg=legend([Line1, Line2, Line3],... 
      'Line1 text','Line2 text','Line3 text',... 
      'Location','NorthEast'); 

回答

5

你可以找到Leg的子女,搜索那些有他们的Type设置为text并重新安置他们的人。这里是一个代码来展示如何做到这一点。它将它们移动到相对于图例框的0.2左边。

ch = get(Leg, 'Children'); 
textCh = ch(strcmp(get(ch, 'Type'), 'text')); 
for iText = 1:numel(textCh) 
    set(textCh(iText), 'Position', get(textCh(iText), 'Position') + [-0.2 0 0]) 
end 
+0

谢谢!这正是我想要做的。 – mrsoltys

+0

很高兴能帮到你! –

+0

这在R2015a中不起作用 – Veridian

0

我很好奇,为什么你要做到这一点,但一个可能的解决方案可能是:

clf; 
hold on; 
x=0:0.1:2*pi; 
plot(x,sin(x),'s'); 
plot(x,cos(x),'o'); 
ax=legend('sin','cos'); 
LEG = findobj(ax,'type','text'); 
set(LEG,'HorizontalAlignment','center') 

您可以测试出'center''right',并可使用任何工作。如果两者都不起作用,请忽略我的回答

+0

谢谢,好主意,但这会导致图例文字与符号重叠。我只是想提高我的情节的可读性,我觉得就目前而言,传奇文字和符号之间只有太多的空间 – mrsoltys