MATLAB画双纵坐标plotyy的用法对数坐标
MATLAB画双纵坐标
具有两个纵坐标标度的图形
在MA TLAB中,如果需要绘制出具有不同纵坐标标度的两个图形,可以使用plotyy绘图函数。调用格式为:
plotyy(x1,y1,x2,y2)
其中x1,y1对应一条曲线,x2,y2对应另一条曲线。横坐标的标度相同,纵坐标有两个,左纵坐标用于x1,y1数据对,右纵坐标用于x2,y2数据对。
双y轴坐标可以用plotyy(x,y1,x,y2)来实现
双x坐标可以用
set(gca,'xaxislocation','bottom','xticklabel',{'0','1','2','3','4'}) (假设x轴的标注为1,2,3,4)set(gca,'xaxislocation','top','xticklabel',{'0','1','2','3','4'})
进行相应的设置
【* 例10.7.3 -1 】制作一个双坐标系用来表现高压和低温两个不同量的过渡过程。
tp=(0:100)/100*5;yp=8+4*(1-exp(-0.8*tp).*cos(3*tp)); % 压力数据
tt=(0:500)/500*40;yt=120+40*(1-exp(-0.05*tt).*cos(tt)); % 温度数据
% 产生双坐标系图形
clf reset,h_ap=axes('Position',[0.13,0.13,0.7,0.75]); %<4>
set(h_ap,'Xcolor','b','Ycolor','b','Xlim',[0,5],'Ylim',[0,15]);
nx=10;ny=6; %<6>
pxtick=0:((5-0)/nx):5;pytick=0:((15-0)/ny):15; %<7>
set(h_ap,'Xtick',pxtick,'Ytick',pytick,'Xgrid','on','Ygrid','on')
h_linet=line(tp,yp,'Color','b'); %<9>
set(get(h_ap,'Xlabel'),'String',' 时间/rightarrow (分)')
set(get(h_ap,'Ylabel'),'String',' 压力/rightarrow(/times10 ^{5} Pa )')
h_at=axes('Position',get(h_ap,'Position')); %<12>
set(h_at,'Color','none','Xcolor','r','Ycolor','r'); %<13>
set(h_at,'Xaxislocation','top') %<14>
set(h_at,'Yaxislocation','right','Ydir','rev') %<15>
set(get(h_at,'Xlabel'),'String','/fontsize{15}/fontname{ 隶书} 时间/rightarrow (分)')
set(get(h_at,'Ylabel'),'String',' ( {/circ}C )/fontsize{15} /leftarrow /fontname{ 隶书} 零下温度')
set(h_at,'Ylim',[0,210]) %<18>
line(tt,yt,'Color','r','Parent',h_at) %<19>
xpm=get(h_at,'Xlim'); %<20>
txtick=xpm(1):((xpm(2)-xpm(1))/nx):xpm(2); %<21>
tytick=0:((210-0)/ny):210; %<22>
set(h_at,'Xtick',txtick,'Ytick',tytick) %<23>
图10.7.3 -1 双坐标系图形
https://www.doczj.com/doc/a814848513.html,/jinger1985/246333/Message.aspx
实现双纵坐标画图,其中一个为对数坐标,另一个为正常坐标。而且两个坐标的范围差别很大
举例如下:
t = 0:900; A = 1000; a = 0.005; b = 0.005;
z1 = A*exp(-a*t);
z2 = sin(b*t);
[haxes,hline1,hline2] = plotyy(t,z1,t,z2,'semilogy','plot');
https://www.doczj.com/doc/a814848513.html,/archiver/tid-738830.html
matlab作图里面如何分别设置双纵坐标的刻度?工作遇到如下问题:
需要设置双y轴的刻度,用到以下函数,
set(gca,'XTick',[0:5:100])
set(gca,'yTick',[0:10:350])
只是设置左边的y轴刻度,请问各位高手,右边y轴怎么设置刻度标注?
双纵坐标的标注已实现
[AX]=plotyy(x1,y1,x1,y2);
set(get(AX(1),'Ylabel'),'string','left Y-axis‘);
set(get(AX(2),'Ylabel'),'string','right y-axis');
了解plotyy的返回值
[AX]=plotyy(x1,y1,x1,y2);
得到两个axes句柄,AX(1)和AX(2)
set(AX(1),'yTick',[0:10:350]) 设置左边Y轴的刻度
set(AX(2),'yTick',[0:10:350]) 设置右边Y轴的刻度
https://www.doczj.com/doc/a814848513.html,/thread-42331-1-1.html
双y坐标实例
close all hidden
clear all
clc
% w=boxcar(nfft);
fni1=input('请输入时间序列文件: ','s');
fid1=fopen(fni1,'r');
s=fscanf(fid1,'%s',1);
if same(s,'Curve')
for i=1:61
tline=fgetl(fid1);
end
else
fid1=fopen(fni1,'r');
end
a1=fscanf(fid1,'%f');
status=fclose(fid1);
n=length(a1);
n2=n/2;
a2=reshape(a1,2,n2);
x1=a2(1,:);
y1=a2(2,:);
fni2=input('输入速度曲线文件','s');
fid2=fopen(fni2,'r');
b=fscanf(fid2,'%f');
n3=length(b);
n4=n3/2;
b2=reshape(b,2,n4);
x2=b2(1,:);
y2=b2(2,:);
p=polyfit(x2,y2,3);
y3=polyval(p,x2);
% plot(x2,y2)
[AX,H1,H2]=plotyy(x1,y1,x2,y3);
grid on;
xlabel('时间/s');
set(get(AX(1),'Ylabel'),'string','加速度/g');
set(get(AX(2),'Ylabel'),'string','速度km/h');
set(AX(1),'yTick',[-2:0.5:2]);
% % axes1 = axes('Position',[0.08 0.73 0.38 0.25],'Parent',figure1);
% axis(axes1,[0 xtime(end) -0.5 0.5]);
% set(AX(2),'YTick',[300:5:350]);
yticks2 = linspace(300,360,9);
set(AX(2),'YLim',[300 360],'YTick',yticks2);
set(H2,'linewidth',3);
https://www.doczj.com/doc/a814848513.html,/forum/thread-55164-1-1.html
matlab中plotyy双纵坐标函数的具体使用方法如下:
稍加改动和注释。
tp=(0:100)/100*5;
yp=8+4*(1-exp(-0.8*tp).*cos(3*tp)); % 压力数据
tt=(0:500)/500*40;
yt=120+40*(1-exp(-0.05*tt).*cos(tt)); % 温度数据
% 产生双坐标系图形
%clf reset,
figure %打开一个新的图形窗口
%第一个图形
h_ap=axes('Position',[0.13,0.13,0.7,0.75]); %定义了图形的区域位置
set(h_ap,'Xcolor','b','Ycolor','b','Xlim',[0,5],'Ylim',[0,15]);%定义了图形x y坐标轴的颜色和范围nx=10;ny=6; %<6>
pxtick=0:((5-0)/nx):5;pytick=0:((15-0)/ny):15; %<7>
set(h_ap,'Xtick',pxtick,'Ytick',pytick,'Xgrid','on','Ygrid','on')%定义了图形的栅格
h_linet=line(tp,yp,'Color','b'); %<9>%绘制图形
set(get(h_ap,'Xlabel'),'String',' 时间/rightarrow (分)')%设置x坐标轴的标注
set(get(h_ap,'Ylabel'),'String',' 压力/rightarrow(/times10 ^{5} Pa )')%设置y坐标轴的标注
%第二个图形
h_at=axes('Position',get(h_ap,'Position')); %定义第二个图形并参考第一个图形的区域位置
set(h_at,'Color','none','Xcolor','r','Ycolor','r'); %
set(h_at,'Xaxislocation','top') %<14>%x坐标轴在上面
set(h_at,'Yaxislocation','right','Ydir','rev') %y坐标轴在右边
set(get(h_at,'Xlabel'),'String','/fontsize{15}/fontname{ 隶书} 时间/rightarrow (分)') set(get(h_at,'Ylabel'),'String',' ( {/circ}C )/fontsize{15} /leftarrow /fontname{ 隶书} 零下温
度')
set(h_at,'Ylim',[0,210]) %定义y坐标范围
line(tt,yt,'Color','r','Parent',h_at) %绘制图形
xpm=get(h_at,'Xlim'); %获得第二个图形的x范围
txtick=xpm(1):((xpm(2)-xpm(1))/nx):xpm(2); %定义x刻度
tytick=0:((210-0)/ny):210; %定义y刻度
set(h_at,'Xtick',txtick,'Ytick',tytick) %绘制栅格
https://www.doczj.com/doc/a814848513.html,/article/sort010/info-367.html
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
set(AX(1),'XColor','k','YColor','b');
set(AX(2),'XColor','k','YColor','r');
HH1=get(AX(1),'Ylabel');
set(HH1,'String','Left Y-axis');
set(HH1,'color','b');
HH2=get(AX(2),'Ylabel');
set(HH2,'String','Right Y-axis');
set(HH2,'color','r');
set(H1,'LineStyle','-');
set(H1,'color','b');
set(H2,'LineStyle',':');
set(H2,'color','r');
legend([H1,H2],{'y1 = 200*exp(-0.05*x).*sin(x)';'y2 = 0.8*exp(-0.5*x).*sin(10*x)'}); xlabel('Zero to 20 \musec.');
title('Labeling plotyy');
https://www.doczj.com/doc/a814848513.html,/wang1901/blog/item/def559ed01535fd1b31cb103.html
MATLAB作图:plotyy使用方法。即:横坐标相同,纵坐标不同的两条曲线画在同一图上进行比对。
例1. 需要采用图形句柄,详细内容参考MA TLAB帮助文件有关plotyy的例程
%%This example graphs two mathematical functions using plot as the plotting function. The two y-axes
%%enable you to display both sets of data on one graph even though relative values of the data are quite
%%different.
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
用[AX,H1,H2]=plotyy(x,y1,x,y2);命令。AX(1),AX(2)分别为左右Y轴的句柄
%%You can use the handles returned by plotyy to label the axes and set the line styles used for
plotting.
%%With the axes handles you can specify the YLabel properties of the left- and right-side y-axis: set(get(AX(1),'Ylabel'),'String','Slow Decay')
set(get(AX(2),'Ylabel'),'String','Fast Decay')
%%Use the xlabel and title commands to label the x-axis and add a title:
xlabel('Time (\musec)')
title('Multiple Decay Rates')
%%Use the line handles to set the LineStyle properties of the left- and right-side plots:
set(H1,'LineStyle','--')
set(H2,'LineStyle',':')
---------------------------------------------------------------------------------------
例2. 线型设置:
t=0:.1:8;
[ax,h1,h2]=plotyy(t,sin(t),t,cos(t));
set(h1,'linestyle','-','marker','o','color','r');
set(h2,'linestyle',':','marker','x','color','b');
加注图例:
x=linspace(0,2*pi,40);
[ax,h1,h2]=plotyy(x,sin(x)+cos(x),x,exp(x));
set(h1,'linestyle','-')
set(h2,'linestyle','-')
set(h1,'marker','o')
set(h2,'marker','+')
hold on
x=linspace(0,2*pi,40);
hh=line(x,cos(x));
set(hh,'linestyle','-')
set(hh,'marker','s')
hold on
hhf=line(x,sin(x));
set(hhf,'color','r')
set(hhf,'linestyle','-')
set(hhf,'marker','*')
legend([h1,h2,hh,hhf],'sin(x)+cos(x)','exp(x)','cos(x)','sin(x)',0 );
坐标轴标注:
figure;
t=0:.1:3*pi;
[H,Ha,Hb]=plotyy(t,sin(t),t,exp(t));
d1=get(H(1),'ylabel');
set(d1,'string','yayacpf');
d2=get(H(2),'ylabel');
set(d2,'string','bbs from hit','fontsize',18);
https://www.doczj.com/doc/a814848513.html,/wang1901/blog/item/def559ed01535fd1b31cb103.html
matlab中如何显示plotyy的legend
举例:
[ax,h1,h2]=plotyy(x1,y1,x2,y2);
hold on;
h3=plot(x3,y3);
legend([h1,h2,h3],'a','b','c');
即可!
https://www.doczj.com/doc/a814848513.html,/chenlijia_just/blog/static/19616960200811505348965/
双Y轴绘图:plotyy()函数.
其调用格式为:
plotyy(x1,y1,x2,y2)------绘制由x1,y1和x2,y2确定的两组曲线,其中x1,
y1的坐标轴在图形窗口的左侧,x2,y2的坐标轴在
图形窗口的右侧.
Plotyy(x1,y1,x2,y2, 'function1','function2')------功能同上,function是指那些
绘图函数如:plot,semilogx,
loglog等. ...
https://www.doczj.com/doc/a814848513.html,/tag/matlab/