2014-04-17 140 views
1

我想繪製一個圖與五組數據與subplot是有個別的傳說,但問題是軸不匹配時,我試圖把傳說的情節有不同長度的文本。在Matlab繪圖圖例對齊圖

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」 [當劇情外]得到分配的傳奇右對齊所有的傳說。我想要的數據具有相等的座標軸長度或左對齊的圖例。

是否有可能獲得與左對齊的數據,而不包括在軸的長度...?

回答

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

該解決方案是非常相似的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));