2013-05-02 35 views
1

我想添加第二個x軸的數字。它似乎在工作,但第二個軸標籤顯示在圖的半個外面。也就是說,我只看到顯示「第二軸」的下半部分。下面是一個小例子來演示這個問題:第二個x軸標籤不適合裏面圖

close all; 

ax1 = gca; 
set(ax1,'XColor','r','YColor','r') 
xlabel(ax1, '1st Axis'); 


data=rand(10,2); 
line(data(:,1), data(:,2), 'Color', 'r'); 

ax2 = axes('Position',get(ax1,'Position'),... 
      'XAxisLocation','top',... 
      'YAxisLocation','right',... 
      'Color','none',... % necessary, or the axes do not appear 
      'XColor','k','YColor','k'); 
xlabel(ax2, '2nd Axis'); 

data=rand(10,2); 
line(data(:,1), data(:,2), 'Color', 'k','Parent', ax2); 

有沒有更好的方法來定位除'頂部'之外的軸標籤?或者有沒有辦法說「把所有東西都放在圖中」?

回答

0

這個命令將幫助您:
'ActivePositionProperty','OuterPosition'
更多的信息,請參閱this website
你必須調整上軸,使用這個命令:

ax2 = axes('Position',get(ax1,'Position'),... 
     'XAxisLocation','top',... 
     'YAxisLocation','right',... 
     'Color','none',... % necessary, or the axes do not appear 
     'XColor','k','YColor','k','ActivePositionProperty','OuterPosition'); 

如果不是重要的,其中責令情節組裝,反其道而行,使紅軸適合另一個:

close all; 

ax2 = axes('XAxisLocation','top',... 
      'YAxisLocation','right',... 
      'XColor','k','YColor','k','ActivePositionProperty','OuterPosition'); 
xlabel(ax2, '2nd Axis'); 

data=rand(10,2); 
line(data(:,1), data(:,2), 'Color', 'k','Parent', ax2); 

ax1 = axes('Position',get(ax2,'Position'),... 
      'Color','none',... % necessary, or you do not see the second graph 
      'XColor','r','YColor','r'); 
xlabel(ax1, '1st Axis'); 

data=rand(10,2); 
line(data(:,1), data(:,2), 'Color', 'r'); 
+0

嗯,我很高興與實際軸身在何處,只是沒有軸標籤。使用ActivePositionProperty似乎將標籤放在了一個更好的位置(現在完全位於該圖的內部),但它將軸移動到情節的白色部分內,而不僅僅是在邊緣上。 – 2013-05-02 18:31:30

+0

您的編輯工作中的完整代碼,但我不明白區別?爲什麼繪製軸的順序會影響標籤的位置? – 2013-05-02 19:02:36

0

如果有辦法將xLabel設置爲最佳位置,我不會相信它,因爲我不信任legend('','Location','best')。

兩種方法在我的腦海:

set(get(ax2,'XLabel'),'Position',get(xlabh,'Position) - [0 .2 0]) 

ax1 = axes('XColor','r','YColor','r','Position',[0.1 0.1 0.9 0.7]); 
+0

嗯,類似的東西似乎移動的標籤,但我似乎無法得到它在正確的位置: xlab2h = xlabel(ax2,'2nd Axis'); set(xlab2h,'Position',get(xlab2h,'Position') - [0 .09 0]) 使用.1它太遠了,使用.09它在白色區域內是一半, .08它消失了?同樣調整這個值似乎也會改變x的位置? – 2013-05-02 19:00:52