MATLAB画双纵坐标
具有两个纵坐标标度的图形
在MATLAB中,如果需要绘制出具有不同纵坐标标度的两个图形,可以使用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'})
进行相应的设置
实现双纵坐标画图,其中一个为对数坐标,另一个为正常坐标。而且两个坐标的范围差别很大
举例如下:
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');
forum.simwe/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轴的刻度
www.ilovematlab/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')
linspace函数调用的格式为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);
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');
MATLAB作图:plotyy使用方法。即:横坐标相同,纵坐标不同的两条曲线画在同一图上进行比对。
例1. 需要采用图形句柄,详细内容参考MATLAB帮助文件有关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);