在MATLAB中绘制多纵坐标图
上一篇 / 下一篇 2007-04-22 09:13:37 / 天气: 晴朗 / 心情: 高兴 / 个人分类:MATLAB
有网友问到这个问题。感谢bainhome 和shunfly 提供答案。SimWe个人空间Di\&o'i(Q:}
SimWe个人空间i'Z#QCMGfunction [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels)
x%Yr!eu_T2t0%PLOTYYY - Extends plotyy to include a third y-axis SimWe个人空间-nxYk`8n]/x
% SimWe个人空间5sER-m7dQtY
%Syntax: [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels) SimWe个人空间%Sk%I'L mL9S
%
fT?`5NeG;fr0%Inputs: x1,y1 are the xdata and ydata for the first axes' line
C PR`$TH0D0% x2,y2 are the xdata and ydata for the second axes' line SimWe个人空间5au
hC"~G}hs`
% x3,y3 are the xdata and ydata for the third axes' line SimWe个人空间*|j-w/a.Ha2F
% ylabels is a 3x1 cell array containing the ylabel strings
+QR)q*k'Q,O0% SimWe个人空间 \oI%d
B
%Outputs: ax - 3x1 double array containing the axes' handles SimWe个人空间;cd
SAj;y+}O[u
% hlines - 3x1 double array containing the lines' handles SimWe个人空间%^}luW
%
7@e0J3o5M'w~:h0%Example: SimWe个人空间.tY
|-t-Rx
% x=0:10; SimWe个人空间&rcU"]-pgZb*n*z
% y1=x; y2=x.^2; y3=x.^3;
ip&TH3CI _;s0% ylabels{1}='First y-label';
'd%Ij6e|"p0% ylabels{2}='Second y-label'; SimWe个人空间?/n^[h$L V
Qp
% ylabels{3}='Third y-label';
kx Y0\3FL5x0% [ax,hlines] = plotyyy(x,y1,x,y2,x,y3,ylabels);
vm-k&RD8t;C
M0% legend(hlines, 'y = x','y = x^2','y = x^3',2) SimWe个人空间9{%eC"xX8BrQ Z
% SimWe个人空间M[4FUl'Ya
%m-files required: none SimWe个人空间(l4UovA1o~N#r@
?DZy,QF-N0%Author: Denis Gilbert, Ph.D., physical oceanography SimWe个人空间:]P{ls+D3f
%Maurice Lamontagne Institute
Ny ~G6qfD0%Dept. of Fisheries and Oceans Canada SimWe个人空间_o9tOy4|_y
%email: [email]gilbertd@dfo-mpo.gc.ca[/email]
8S7Ocjg1H`6Y U*k0%Web: [url]http://www.qc.dfo-mpo.gc.ca/iml/[/url] SimWe个人空间'X(T+efn
%April 2000; Last revision: 06-Nov-2001 SimWe个人空间9{X/D*` P"n
SimWe个人空间t(OAN};c
if nargin==6 SimWe个人空间!o-X6a!R,s0d
zj-k
%Use empty strings for the ylabels
I^;Zw7j?`+X0 ylabels{1}=' '; ylabels{2}=' '; ylabels{3}=' '; SimWe个人空间5}OP%H M.G4p el
elseif nargin > 7
Y_J/_+BE0 error('Too many input arguments')
r ~&k n)]w0elseif nargin < 6
A}:L0aF0 error('Not enough input arguments') SimWe个人空间"L1kq.{7?*^8D5I
end SimWe个人空间1o1J%C!a0Ugqy*KH
SimWe个人空间0L4};x5Ag,c/kK/H7v0}
figure('units','normalized',...
+^v2UvZ0 'DefaultAxesXMinorTick','off','DefaultAxesYminorTick','off');
m][4fZvr'x r0
TC._9L!z0%Plot the first two lines with plotyy SimWe个人空间-X"iQ_*a A
[ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2); SimWe个人空间i0Ce0z;]
@e-f0[
cfig = get(gcf,'color');
1F$HWhy ^-b1a0pos = [0.1 0.1 0.7 0.8];
Z6jV$N~%G0offset = pos(3)/5.5;
0v*u!t
^%sj.@}Qj0SimWe个人空间U/n2R1j!n^D
%Reduce width of the two axes generated by plotyy
t Y
P3[(M1P l-@0pos(3) = pos(3) - offset/2; SimWe个人空间&FdVZ}C;Td2\:\
set(ax,'position',pos); SimWe个人空间7nX@jW
SimWe个人空间~W&J/D1O'S/V
\
%Set the position of the third axes
`}*{+MB(U;CFoa/Q0pos3=[pos(1) pos(2) pos(3)+offset pos(4)]; SimWe个人空间,RpF0Vi
SimWe个人空间;_'iCI Okd,v
limx1=get(ax(1),'xlim');
{Y WH(d1_0xticks=get(ax(1),'xtick'); SimWe个人空间g'hl&G4_