2015-01-31 71 views
0

我试图使用plotyy何时调整数字大小:如何在使用plotyy时调整图形大小?

clc; 
clear all; 

t = 0:.1:4*pi; 
y = sin(t); 

figure(1); 
set(gcf,'units','inches','renderer', 'painters'); 
pos = get(gcf,'pos'); 
set(gcf,'Units','inches',... 
    'Position',[pos(1) pos(2) 4 2]); 
plot(t,y) 
xlabel('Time(s)') 
ylabel('y(t)') 
title('Sin function') 
legend('y=sin(t)') 
axis([0 t(end) -1.5 1.5]) 
set(gca,... 
    'Units','normalized',... 
    'YTick',-1.5:.5:1.5,... 
    'XTick',0:t(end)/4:t(end),... 
    'FontUnits','points',... 
    'FontWeight','normal',... 
    'FontSize',9,... 
    'FontName','Times') 
set(gca, 'Position', get(gca, 'OuterPosition') - ... 
    get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]); 

figure(2); 
set(gcf,'units','inches','renderer', 'painters'); 
pos = get(gcf,'pos'); 
set(gcf,'Units','inches',... 
    'Position',[pos(1) pos(2) 4 2]); 
[haxes,hline1,hline2]=plotyy(t,y,t,t); 
ylabel(haxes(1),'sin(t)') 
ylabel(haxes(2),'45degree') 
xlabel(haxes(1),'Time(s)') 
title('Sin function') 
set(haxes,... 
    'Units','normalized',... 
    'FontUnits','points',... 
    'FontWeight','normal',... 
    'FontSize',9,... 
    'FontName','Times') 
set(haxes(1), 'Position', get(haxes(1), 'OuterPosition') - ... 
    get(haxes(1), 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 0 0; 0 0 0 1]-... 
    get(haxes(2), 'TightInset') * [0 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 0]); 

实例表明,该过程可以很好地用于情节而不是plotyy。看起来,当我使用plot时,TightInset考虑了底部和顶部的文本(应该是这样),但是当我使用plotyy时,它不考虑它们。但我不明白为什么会出现这种情况,以及如何解决这个问题。有任何想法吗?

[下面的代码是关系到我的评论所选择的答案]

figure(3); 
set(gcf,'units','inches','renderer', 'painters'); 
pos = get(gcf,'pos'); 
set(gcf,'Units','inches',... 
    'Position',[pos(1) pos(2) 4 2]); 
plot(t,y,'b'); 
set(gca,'YColor','b') 
xlabel('Time(s)') 
ylabel('y(t)') 
title('Sin function') 
axis([0 t(end) -1.5 1.5]); 
set(gca,... 
    'Units','normalized',... 
    'YTick',-1.5:.5:1.5,... 
    'XTick',0:t(end)/4:t(end),... 
    'FontUnits','points',... 
    'FontWeight','normal',... 
    'FontSize',9,... 
    'FontName','Times') 
axesPosition = get(gca,'Position'); 
hNewAxes = axes('Position',axesPosition,... 
    'Color','none',...   
    'YLim',[0 10],...    
    'YAxisLocation','right',... 
    'XTick',[],...    
    'Box','off');     
set(gca,'YColor',[0 .5 0]); 
ylabel(hNewAxes,'45degree'); 
hold all 
plot(hNewAxes,t,t,'color',[0 .5 0]); 
hold off 

回答

0

我个人尽量避免plotyy因为总是有预期不行为的属性。如果我需要,我通常会创建第二个axes并手动设置属性,当然需要从第一个axes复制我需要的内容,并链接一些属性。

如果你看看plotyy的代码,你会发现它的确如此......还有一些额外的东西。

一个不错的额外是如何试图同时匹配y轴蜱,甚至当他们的跨度是不同的(但你可以复制的那部分,仍然采用手动双轴溶液)。

另一个额外的,我怀疑是你观察到的行为负责,是它设置监听和回调重新调整两个轴的position属性。因为如果您在代码中注意到,即使在您尝试手动更改position之前,在调用xlabeltitle时也不会重新调整axes(因为它们通常在您执行操作时那在一个标准的单一的axes)。

这就是为什么TightInset您尝试使用重新调整你的axes不工作...因为当你添加的文本对象axes没有调整,(所以垂直TightInset是错误的)。


现在对于你的情况,幸好这似乎只影响TightInset的垂直部分,所以一个快速的解决方法得到的一切紧张,因为在你的第一个数字是使用OuterPosition属性的垂直部分(后从水平部分获得TightInset的边距)。

因此,在你的代码只是更换您上次调用set(haxes,'Position', ...)

margin = get(haxes(1),'TightInset') * [0;0;1;0] ; 
set(haxes,'OuterPosition',[-margin 0 1+margin 1]) 

,一切都应该再紧;-)

+0

完美!我也尝试了另一个手动设置另一个轴的建议。令人惊讶的是,没有必要调整位置以使所有事情变得紧张!我完全不知道为什么这个“神奇”发生。你知道这是为什么吗? (因为我不能在这里放置一个代码,所以我已经将它添加到了这个问题中) – capadocia 2015-02-01 02:00:04

相关问题