常用Matlab作图命令1.概率统计作图1.1绘出正态分布的密度函数曲线-5-4-3-2-101234500.050.10.150.20.250.30.350.4N(0,1)N(0,2)正态分布密度曲线x=-5:0.1:5;y=normpdf(x,0,1);z=normpdf(x,0,2);plot(x,y,x,z)gtext('N(0,1)')gtext('N(0,2)')title('正态分布密度曲线')1.2绘出t-分布的密度函数曲线,并与标准正态密度曲线比较-5-4-3-2-101234500.050.10.150.20.250.30.350.4x概率密度pt分布标准正态密度x=-5:0.1:5;y=tpdf(x,30);z=normpdf(x,0,1);plot(x,y,'k:',x,z,'k-')xlabel('\itx');ylabel('概率密度\itp')legend('t分布','标准正态密度')difference=tpdf(x,30)-normpdf(x,0,1)1.3绘制开方分布密度函数在n分别等于1、5、15的图x=0:1:30;y1=chi2pdf(x,1);plot(x,y1,':')holdony2=chi2pdf(x,5);plot(x,y2,'+')y3=chi2pdf(x,15);plot(x,y3,'O')Axis([0,30,0,0.2])1.4计算自由度是50,10的F-分布的0.9的分位数,并给出概率与分位数关系的图形x=finv(0.9,50,10)x=2.1171p=fcdf(x,50,10)p=0.9000t=0:0.1:4;y=fpdf(x,50,10);z=fpdf(t,50,10);plot(t,z,[x,x],[0,y])text(x,0,'2.1171')gtext('p=0.9')title('概率与分位数的关系')1.5经验累积分布函数图形X=normrnd(0,1,50,1);[h,stats]=cdfplot(X)-2.5-2-1.5-1-0.500.511.500.10.20.30.40.50.60.70.80.91xF(x)EmpiricalCDF00.511.522.533.5400.10.20.30.40.50.60.70.80.92.1171p=0.9概率与分位数的关系05101520253000.020.040.060.080.10.120.140.160.180.2y=evrnd(0,3,100,1);cdfplot(y)holdonx=-20:0.1:10;f=evcdf(x,0,3);plot(x,f,'m')legend('Empirical','Theoretical','Location','NW')1.6绘制正态分布概率图形-2.5-2-1.5-1-0.500.511.50.010.020.050.100.250.500.750.900.950.980.99DataProbabilityNormalProbabilityPlotX=normrnd(0,1,50,1);normplot(X)1.7绘制威布尔(Weibull)概率图形%绘制威布尔(Weibull)概率图形的目的是用图解法估计来自威布尔分布的数据X,如果X是威布%尔分布数据,其图形是直线的,否则图形中可能产生弯曲。r=weibrnd(1.2,1.5,50,1);weibplot(r)-20-15-10-5051000.10.20.30.40.50.60.70.80.91xF(x)EmpiricalCDFEmpiricalTheoretical10-11000.010.020.050.100.250.500.750.900.960.99DataProbabilityWeibullProbabilityPlot1.8样本数据的盒图%boxplot(X)%产生矩阵X的每一列的盒图和“须”图,“须”是从盒的尾部延伸出来,并表示盒外数据长度的线,如果“须”的外面没有数据,则在“须”的底部有一个点。x1=normrnd(5,1,100,1);x2=normrnd(6,1,100,1);x=[x1x2];boxplot(x,1,'g+',1,0)1.9样本的概率图形data=normrnd(0,1,30,2);p=capaplot(data,[-2,2])p=0.91991.10附加有正态密度曲线的直方图r=normrnd(10,1,100,1);histfit(r)1.11在指定的界线之间画正态密度曲线格式p=normspec(specs,mu,sigma)%specs指定界线,mu,sigma为正态分布的参数p为样本落在上、下界之间的概率12345678ValuesColumnNumber-4-3-2-10123400.050.10.150.20.250.30.35ProbabilityBetweenLimits=0.86783789101112131405101520257891011121314151600.050.10.150.20.250.30.35ProbabilityGreaterthanLowerBoundis0.88493DensityCriticalValuenormspec([10Inf],11.5,1.25)1.12二项分布的函数图p=0.2;%Probabilityofsuccessforeachtrialn=10;%Numberoftrialsk=0:n;%Outcomesm=binopdf(k,n,p);%Probabilitymassvectorbar(k,m)%Visualizetheprobabilitydistributionset(get(gca,'Children'),'FaceColor',[.8.81])gridon1.13指数分布函数图lambda=2;%Failureratet=0:0.01:3;%Outcomesf=exppdf(t,1/lambda);%Probabilitydensityvectorplot(t,f)%Visualizetheprobabilitydistributiongridon1.14ksdensity概率密度估计函数01234567891000.050.10.150.20.250.30.3500.511.522.5300.20.40.60.811.21.41.61.82cars=load('carsmall','MPG','Origin');MPG=cars.MPG;[f,x,u]=ksdensity(MPG);plot(x,f)title('DensityestimateforMPG')holdon[f,x]=ksdensity(MPG,'width',u/3);plot(x,f,'r');[f,x]=ksdensity(MPG,'width',u*3);plot(x,f,'g');legend('defaultwidth','1/3default','3*default')holdoff-10010203040506000.0050.010.0150.020.0250.030.0350.040.045normalepanechnikovboxtrianglehname={'normal''epanechnikov''box''triangle'};colors={'r''b''g''m'};forj=1:4[f,x]=ksdensity(MPG,'kernel',hname{j});plot(x,f,colors{j});holdon;endlegend(hname{:});holdoff2.常用作图函数2.1普通双函数图t=0:pi/20:2*pi;y=exp(sin(t));plotyy(t,y,t,y,'plot','stem')0123456700.511.522.53XAxisPlotYAxisTwoYAxes0123456700.511.522.53-40-2002040608010000.010.020.030.040.050.06DensityestimateforMPGdefaultwidth1/3default3*defaultxlabel('XAxis')ylabel('PlotYAxis')title('TwoYAxes')2.2多数据集在同一图中01234567-1-0.8-0.6-0.4-0.200.20.40.60.81sin(x)sin(x-.25)sin(x-.5)x=0:pi/100:2*pi;y=sin(x);y2=sin(x-.25);y3=sin(x-.5);plot(x,y,x,y2,x,y3)legend('sin(x)','sin(x-.25)','sin(x-.5)')2.3在原图上继续作图holdon[x,y,z]=peaks;pcolor(x,y,z)shadinginterpholdoncontour(x,y,z,20,'k')holdoff2.4同一图中作多个图051015-1-0.8-0.6-0.4-0.200.20.40.60.81051015-0.4-0.200.20.40.60.81clearx=0.1:0.1:4*pi;%生成向量x。y1=sin(x);%生成y1值y2=sin(x)./x;%生成y2值。figure;%创建一个新窗口。subplot(1,2,1);%定义第一个子图区域。plot(x,y1);%用实线画曲线。subplot(1,2,2);%定义第二个子图区域。plot(x,y2,'*');%用‘*’号画曲线02468246810Default137246810Xscalemanipulated02468269Yscalemanipulated137269Bothscalesmanipulatedclear;x=[137];y=[692];s1=subplot(2,2,1);plot(x,y);grid;title('Default');s2=subplot(2,2,2);plot(x,y);set(s2,'XTick',x);%改变X轴标记set(s2,'XGrid','on');%画X轴的格栅线title('Xscalemanipulated');s3=subplot(2,2,3);plot(x,y);set(s3,'YTick',[2,6,9]);%改变y轴标记set(s3,'YGrid','on');%画y轴的格栅线set(s3,'GridLineStyle','-.');%使用虚线格栅title('Yscalemanipulated');s4=subplot(2,2,4);plot(x,y);set(s4,'XTick',x);%改变xy轴标记set(s4,'YTick',[269]);grid;%画xy轴的格栅线title('Bothscalesmanipulated');clfreset%ClearingtheFigureforaNewPlott=0:pi/10:2*pi;[X,Y,Z]=cylinder(4*cos(t));subplot(2,2,1);mesh(X)subplot(2,2,2);mesh(Y)subplot(2,2,3);mesh(Z)subplot(2,2,4);mesh(X,Y,Z)2.5标签的显示(字符参照Latex标准)-3-2-10123-1-0.8-0.6-0.4-0.200.20.40.60.81-tsin(t)GraphofthesinefunctionNotetheoddsymmetry.t=-pi:pi/100:pi;y=sin(t);plot(t,y)axis([-pipi-11])xlabel('-\pi\leq{\itt}\leq\pi')ylabel('sin(t)')title('Graphofthesinefunction')t