【问题标题】:How to adjust figure size when using plotyy?使用plotyy时如何调整图形大小?
【发布时间】:2015-02-01 09:55:49
【问题描述】:

我正在尝试使用 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]);

该示例表明该过程适用于 plot 但不适用于 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

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

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

    如果您查看plotyy 的代码,您会发现它的作用相同......还有一些额外的。

    一个不错的附加功能是它如何尝试匹配两个 y 轴上的刻度,即使它们的跨度不同(但您可以复制该部分并仍然使用手动双轴解决方案)。

    另一个额外的,我怀疑是造成您观察到的行为的原因是,它设置了侦听器和回调来重新调整两个轴的 position 属性。在某个地方,它必须终止对axes 的一些自动重新调整,因为如果您在代码中注意到,即使在您尝试手动更改position 之前,当您调用xlabel 和@ 时,axes 也没有重新调整。 987654331@(通常是在标准单曲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])
    

    一切都应该再次紧张;-)

    【讨论】:

    • 完美!我还尝试了您手动设置另一个轴的其他建议。令人惊讶的是,根本不需要调整位置来让一切都紧绷!我完全不知道为什么会发生这种“神奇”的事情。你知道为什么会这样吗? (由于我无法在此处输入代码,因此我已将其添加到问题中)
    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    相关资源
    最近更新 更多