Matlab图中图例的对齐

时间:2014-04-17 10:06:08

标签: matlab plot legend

我试图绘制一个带有五组数据的图形,子图集具有单独的图例,但问题是当我试图将图表的图例具有不同长度的文本时,轴会变得不匹配。

x = [1:10];
y = 2*x;
z = x+1.25*y;
z1 = z+x;
subplot(4,1,1);
plot(x);
legend('x Variable','Location','NorthEastOutside');
subplot(4,1,2);
plot(y);
legend('y var','Location','NorthEastOutside');
subplot(4,1,3);
plot(z);
legend('z','Location','NorthEastOutside');
subplot(4,1,4);
plot(z1);
legend('z1 point','Location','NorthEastOutside');

enter image description here

所有传说,当位置为'NorthEastOutside'时[在情节之外]被分配右对齐的图例。我希望数据具有相等的轴长度或左对齐的图例。

是否可以使用Left Justified获取数据而不包括轴长...?

2 个答案:

答案 0 :(得分:2)

这是使所有子图的宽度相同的一种解决方案

x = [1:10];
y = 2*x;
z = x+1.25*y;
z1 = z+x;
h(1)=subplot(4,1,1);
plot(x);
legend('x Variable','Location','NorthEastOutside');
h(2)=subplot(4,1,2);
plot(y);
legend('y var','Location','NorthEastOutside');
h(3)=subplot(4,1,3);
plot(z);
legend('z','Location','NorthEastOutside');
h(4)=subplot(4,1,4);
plot(z1);
legend('z1 point','Location','NorthEastOutside');

m=zeros(length(h),4);
for k=1:length(h)
    m(k,:) = get(h(k),'Position');
end

m(:,3) = max(m(:,3));
for k=1:length(h)
    set(h(k),'Position',m(k,:));
end

Plot

答案 1 :(得分:1)

此解决方案与answer of user3544639非常相似,但没有循环且更通用,因为不需要为所有子图提供句柄。

%// get all subplot axes handles of current figure
s = findobj(gcf,'Type','axes','Tag','');
%// get cell array with positions
p = get(s,'position');

%// masking of positons vector
mask = [0 0 1 0];

%// maximum width
max_width = max( cell2mat(p)*mask' );

%// assinging of new width
arrayfun(@(x) set(s(x),'position',p{x}.*~mask + max_width*mask), 1:numel(s));