2013-05-02 53 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