【发布时间】: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