Matlab绘图高级部分
图形是呈现数据的一种直观方式,在用Matlab进行数据处理和计算后,我们一般都会以图形的形式将结果呈现出来。尤其在论文的撰写中,优雅的图形无疑会为文章加分。本篇文章非完全原创,我的工作就是把见到的Matlab绘图代码收集起来重新跑一遍,修改局部错误,然后将所有的图贴上来供大家参考。大家可以先看图,有看中的可以直接把代码Copy过去改成自己想要的。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <br>%% 直方图图的绘制%直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。figure;z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额colormap(cool);% 控制图的用色subplot(2,3,1);bar(z);%二维分组式直方图,默认的为'group'title('2D default');subplot(2,3,2);bar3(z);%三维的分组式直方图title('3D default');subplot(2,3,3);barh(z,1);%分组式水平直方图,宽度设置为1title('vert width=1');subplot(2,3,4);bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bartitle('stack')subplot(2,3,5);bar3h(z,0.5,'stacked');%三维累计式水平直方图title('vert width=1 stack');subplot(2,3,6);bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'title('width=0.8 grouped'); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | %% =========柱状图的进阶==========figure;y=[300311;390425; 312321; 250185; 550535; 420432; 410520;];subplot(1,3,1);b=bar(y);grid on;set(gca,'XTickLabel',{'0','1','2','3','4','5','6'})legend('算法1','算法2');xlabel('x axis');ylabel('y axis');%使仅有的一组柱状图呈现不同颜色,默认的位相同颜色data = [1.0, 1.0, 0.565, 0.508, 0.481, 0.745];subplot(1,3,2);b = bar(data);ch = get(b,'children');set(ch,'FaceVertexCData',[4;2;3;1;5;6]);%使用Indexed形式指定每组bar的颜色set(gca,'XTickLabel',{'C0','C1','C2','C3','C4','C5'})axis([070.01.0]);ylabel('micro F-measure');%使每个bar颜色不同,默认的是每个元素在不同组的颜色相同data = [3, 7, 5, 2;4, 3, 2, 9;6, 6, 1, 4];subplot(1,3,3);b = bar(data);ch = get(b,'children');set(ch{1},'FaceVertexCData',[1;2;3]);%设置第一个元素在不同组的颜色set(ch{2},'FaceVertexCData',[1;2;3]);%设置第二个元素在不同组的颜色set(ch{3},'FaceVertexCData',[1;2;3]);set(ch{4},'FaceVertexCData',[1;2;3]); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | %% 彩色柱状图%用到的数据n = 8;Z = rand(n,1);figure;%默认图片subplot(1,3,1);bar(Z);%简单的作图% 这个图根据数据列中值的大小着色。每列中的值越大,颜色越突出subplot(1,3,2);h=bar(Z);colormap(summer(n));ch = get(h,'Children');fvd = get(ch,'Faces');%针对矩阵时,只能用fvd=get(ch{col},'Faces'),下同fvcd = get(ch,'FaceVertexCData');[~, izs] = sortrows(Z,1);fori = 1:n    row = izs(i);    fvcd(fvd(row,:)) = i;endset(ch,'FaceVertexCData',fvcd)%图片会以渐变的方式着色,效果非常不错subplot(1,3,3);h=bar(Z);ch = get(h,'Children');fvd = get(ch,'Faces');fvcd = get(ch,'FaceVertexCData');[zs, izs] = sortrows(Z,1);k = 128; % 准备生成128*3行的colormapcolormap(summer(k)); % 这样会产生一个128* 3的矩阵,分别代表[R G B]的值% 检视数据whos ch fvd fvcd zs izs%   Name       Size            Bytes  Class     Attributes%%   ch         1x1                 8double%   fvcd      66x1               528double%   fvd       13x4               416double%   izs       13x1               104double%   zs        13x1               104double%shading interp % Needed to graduate colorsfori = 1:n    color = floor(k*i/n); % 这里用取整函数获得color在colormap中行    row = izs(i); % Look up actual row # in data    fvcd(fvd(row,1)) = 1; % Color base vertices 1st index    fvcd(fvd(row,4)) = 1;    fvcd(fvd(row,2)) = color; % Assign top vertices color    fvcd(fvd(row,3)) = color;endset(ch,'FaceVertexCData', fvcd); % Apply the vertex coloringset(ch,'EdgeColor','k'); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | %% 绘制统计直方图%hist(y):如果y是向量,则把其中元素放入10个条目中,且返回每条中的元素的个数;如果y为矩阵,则分别对每列进行处理,显示多组条形。%[n,xout]=hist(y,x):非递减向量x的指定bin的中心。向量xout包含频率计数与条目的位置。x=-10:.1:10;y1=randn(2008,1);y2=randn(2008,3);figure;colormap(winter);subplot(2,2,1);hist(y1);%把其中元素放入10个条目中title('y1为向量,default,n=10');subplot(2,2,2);hist(y2);%分别对每列进行处理,显示多组条形title('y2为矩阵');subplot(2,2,3);hist(y1,x);%用户也可以使用[n,xout]=hist(y1,x);bar(xout,n)绘制条形直方图title('向量x指定条目');subplot(2,2,4);hist(y2,1000);%第二个参数为标量时指定bin的数目title('nbins=1000'); | 

| 1 2 3 4 5 6 7 8 9 | %% ========均值方差直方图========a=[8910789];%meanb=[111111];%stdfigure();h=bar(a);ch=get(h,'children');set(ch,'FaceVertexCData',[4;2;3;1;5;6]);%使用Indexed形式指定每组bar的颜色hold on;errorbar(a,b,'k','LineStyle','none'); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | %% =======散点图scatter , scatter3 , plotmatrix======%scatter3(X,Y,Z,S,C):在由向量X、Y和Z指定的位置显示大小和颜色分别由S和C决定的离散点figure;[x,y,z] = sphere(16);X = [x(:)*.5x(:)*.75x(:)];Y = [y(:)*.5y(:)*.75y(:)];Z = [z(:)*.5z(:)*.75z(:)];S = repmat([1025]*10,numel(x),1);C = repmat([123],numel(x),1);subplot(1,2,1);scatter(X(:),Y(:),S(:),C(:));title('scatter');subplot(1,2,2);scatter3(X(:),Y(:),Z(:),S(:),C(:),'filled'), view(-60,60);title('scatter3');%plotmatrix(X,Y)绘出X(p*M)与Y(p*N)的列组成的散度图(N,M)figure;X=randn(100,2);Y=randn(100,2); subplot(1,3,1),plotmatrix(X);%等价于plotmatrix(X,X),除了对角上的图为X每一列的直方图hist(X(:,col))title('plotmatrix(X)');subplot(1,3,2),plotmatrix(X,X);title('plotmatrix(X,X)');subplot(1,3,3),plotmatrix(X,Y);title('plotmatrix(X,Y)'); | 
| 1 2 3 4 5 6 7 8 9 10 11 | %% =========绘制区域图===========%区域图特点是:在图上绘制多条曲线时,每条曲线(除第一条外)都是把“前”条曲线作基线,再取值绘制而成。因此,该指令所画的图形,能醒目地反映各因素对最终结果的贡献份额。figure;x=1:2:9;% 注意:自变量要单调变化y=magic(5);% 各因素的相对贡献份额,每一列相当于一个因素colormap(spring);% 控制图的用色area(x,y,4);%area(y)则以列下标作为自变量,第三个参数为基准线(默认为0)set(gca,'layer','top');%图层设置为top层,显示网格title('basevalue=4');legend(' 因素 A',' 因素 B',' 因素 C','因素D','因素E');grid on; | 

| 1 2 3 4 5 6 7 8 9 10 | %% =========绘制饼状图=========%饼图指令pie和pie3用来表示各元素占总和的百分数。该指令第二个参数为与第一参数等长的 0-1%向量,1使对应扇块突出。第三个参数指定个扇区的labelfigure;colormap(summer);% 控制图的用色x=[1617212521];subplot(1,2,1);pie(x,[00001],{'0-10岁儿童','10-20岁儿童','20-35岁青年','35-55岁中年','55岁以上老年'});subplot(1,2,2);pie3(x,[00001],{'0-10岁儿童','10-20岁儿童','20-35岁青年','35-55岁中年','55岁以上老年'}); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | %% 绘制填色多边形。若每列的首尾元素不重合,则将默认把最后一点与第一点相连,强行使多边形封闭。%fill和fill3用于绘制填色多边形%fill(X1,Y1,C1,X2,Y2,C2,...)%fill3(X1,Y1,Z1,C1,X2,Y2,Z2,C2,...)%参数1和2为等长向量时,多边形的节点数由项链长度决定;而当其为矩阵时,每一列对应一个多边形%参数3为颜色(用颜色字符r/g/b/c或[r g b]表示)figure;colormap(autumn);% 控制图的用色n=10; % 多边形的边数dt=2*pi/n;t=0:dt:2*pi;t=[t,t(1)]; %fill 指令要求数据向量的首位重合,使图形封闭。x=sin(t);y=cos(t);subplot(1,2,1);fill(x,y,[110]);axis off % 画填色多边形,隐去坐标轴。X=[0.50.50.50.5;0.50.50.50.5;0110]; Y=[0.50.50.50.5;0.50.50.50.5;0011];Z=[1111;0000;0000];C=[1001;0101;0010];subplot(1,2,2);fill3(X,Y,Z,C);view([-1055]);xlabel('x'),ylabel('y');box on;grid on; | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | %% =======绘制离散数据杆状图===========%stem和stem3函数用于绘制二维或三维的离散数据杆状图%stem(Y)可以理解成绘制离散点的plot(y)函数%stem(X,Y)可以理解成绘制离散点的plot(x,y)函数%stem(...,'filled')改变数据点显示的空、实状态。%stem(...,'LINESPEC')Linespec代表直线属性设置参量。x=1:.1:10;y=exp(x.*sin(x));figure;subplot(1,3,1);plot(x,y,'.-r');title('plot(x,y)');subplot(1,3,2);stem(x,y,'b');subplot(1,3,3);stem(x,y,':g','fill');%绘制三维离散杆状图th=(0:127)/128*2*pi;% 角度采样点x=cos(th);y=sin(th);f=abs(fft(ones(10,1),128)); %对离散方波进行 FFT 变换,并取幅值stem3(x,y,f','cd','fill');%绘制图形view([-6530]);xlabel('Real'); %图形标注ylabel('Imaginary');zlabel('Amplitude');title('FFT example'); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | %% =======绘制方向和速度矢量图=======%compass-绘制罗盘图%feather-绘制羽毛图%quiver-绘制二维箭头图%quiver3-绘制三维箭头图%绘制罗盘图figure;wdir=[45909045360335360270335270335335];knots=[668639689101412];rdir=wdir*pi/180;[x,y]=pol2cart(rdir,knots);% 极坐标转化为直角坐标compass(x,y);title('风向和风力')%绘制羽毛图figure;alpha=90:-10:0;r=ones(size(alpha));m=alpha*pi/180;n=r*10;[u,v]=pol2cart(m,n);% 极坐标转化为直角坐标feather(u,v);title('羽毛图')%罗盘图和羽毛图的比较figure;t=-pi/2:pi/12:pi/2; % 在 区间,每 取一点。r=ones(size(t)); % 单位半径[x,y]=pol2cart(t,r); % 极坐标转化为直角坐标subplot(1,2,1),compass(x,y),title('Compass')subplot(1,2,2),feather(x,y),title('Feather')%绘制箭头图figure;[x,y] = meshgrid(-2:.2:2,-1:.15:1);z = x .* exp(-x.^2- y.^2); [px,py] = gradient(z,.2,.15);subplot(1,2,1);contour(x,y,z), hold onquiver(x,y,px,py), hold off, axis imagetitle('quiver示例');[x,y,z]=peaks(15);[nx,ny,nz]=surfnorm(x,y,z);%surfnorm求平面的法向量subplot(1,2,2)surf(x,y,z);hold on;quiver3(x,y,z,nx,ny,nz);title('quiver3示例'); | 


| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | %% ==========轮廓线图的绘制==========%clabel-利用轮廓矩阵生成标签并在当前图形中显示%contour-利用矩阵所给的值生成二维轮廓线%contour3-利用矩阵所给的值生成三维轮廓线%contourf-显示二维轮廓图并用色彩填充个轮廓线的间隙%contourc-计算被其他轮廓函数占用的轮廓矩阵的低层函数[x,y,z]=peaks;n=15;% 等高线分级数figure;subplot(1,3,1);h=contour(x,y,z,n);%绘制20条等高线clabel(h);%当前图形中显示标签,标签前有'+'号且标签会根据轮廓线旋转,每条轮廓线仅有一个标签title('simple contour,n=20');subplot(1,3,2);z=peaks;[c,h]=contour(z,n);%绘制15条等高线clabel(c,h);%标签前无'+'号,每天轮廓线可能有多个标签title('调用clabel函数标注轮廓图')subplot(1,3,3);z=peaks;[c,h]=contourf(z,n);clabel(c,h,'FontSize',15,'Color','r','Rotation',0);%自定义标签colorbar;title('使用自定义标注并彩色填充轮廓线的间隙'); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 | %% ========= Voronoi图和三角剖分========%用Voronoi多边形勾画每个点的最近邻范围。Voronoi多边形在计算几何、模式识别中有重要应用。三角形顶点所在多边形的三条公共边是剖分三角形边的垂直平分线。n=30;A=rand(n,1)-0.5;B=rand(n,1)-0.5; % 产生 30个随机点T=delaunay(A,B); % 求相邻三点组T=[T T(:,1)]; %为使三点剖分三角形封闭而采取的措施voronoi(A,B) % 画 Voronoi 图hold on;axis squarefill(A(T(10,:)),B(T(10,:)),'y'); % 画一个剖分三角形voronoi(A,B) % 重画 Voronoi 图,避免线被覆盖title('Voronoi图和三角剖分'); | 

| 1 2 3 4 5 6 7 8 9 10 | %% =========三角网线和三角曲面图========figure;X=6*pi*(rand(20,10)-0.5);Y=6*pi*(rand(20,10)-0.5); R=sqrt(X.^2+Y.^2)+eps;Z=sin(R)./R;tri=delaunay(X,Y); % 进行三角剖分subplot(1,2,1),trimesh(tri,X,Y,Z);title('三角网线');subplot(1,2,2),trisurf(tri,X,Y,Z);title('三角曲面图');colormap(copper);brighten(0.5) % 增强亮度 | 

| 1 2 3 4 5 6 7 8 9 10 | %% ============彩带图ribbon========%ribbon(X,Y,WIDTH)和plot(X,Y)一样的,只不过每一列在三维中以分开的ribbon绘制figure;x=0:pi/100:2*pi;x=repmat(x',1,10);y=sin(x);ribbon(x,y,0.4);% 画彩带图% 至此彩带图已经生成。以下指令都是为了使图形效果更好、标识更清楚而用。view([150,50]),shading interp,colormap(hot)% 设置视角、明暗、色图light,lighting phong,box on % 设置光源、照射模式、坐标框 | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | %% ==========在特殊坐标系中绘制特殊图形。=======%利用polar函数在极坐标系中绘制图形figure;theta=0:.1:pi;rho1=sin(theta);rho2=cos(theta);subplot(1,3,1);polar(theta,rho1,'.-r');hold on;polar(theta,rho2,'--g');title('极坐标系中绘图');%另外一种和极坐标有关系的坐标系就是柱坐标系了theta=0:pi/100:3*pi;rho=sin(theta)+cos(theta);[t,r]=meshgrid(theta,rho);z=r.*t;subplot(1,3,2);[x,y,z]=pol2cart(t,r,z);%极坐标系向柱坐标系转化mesh(x,y,z);%柱坐标系中进行绘图title('柱坐标系中绘图');view([-6530]);%将球坐标系转换为柱面坐标系subplot(1,3,3);delta=pi/100;theta=0:delta:pi; % theta is zenith anglephi=0:delta:pi; % phi is azimuth angle[t p]=meshgrid(theta,phi);r=ones(size(t));[x,y,z]=sph2cart(t,p,r);%球坐标向柱坐标转化mesh(x,y,z);%球坐标系中进行绘图title('球坐标系中绘图'); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | %% ======四维表现========%用色彩表现函数的特征%当三维网线图、曲面图的第四个输入宗量取一些特殊矩阵时,色彩就能表现或加强函数的某特征,如梯度、曲率、方向导数等。x=3*pi*(-1:1/15:1);y=x;[X,Y]=meshgrid(x,y); R=sqrt(X.^2+Y.^2)+eps;Z=sin(R)./R;[dzdx,dzdy]=gradient(Z);dzdr=sqrt(dzdx.^2+dzdy.^2); % 计算对 r 的全导数dz2=del2(Z); % 计算曲率figure;subplot(1,2,1),surf(X,Y,Z),title('No. 1 surf(X,Y,Z)');shading faceted,colorbar( 'horiz') ,brighten(0.2);subplot(1,2,2),surf(X,Y,Z,R),title('No. 2 surf(X,Y,Z,R)');shading faceted;colorbar( 'horiz');%色彩分别表现函数的高度和半径特征figure;subplot(1,2,1),surf(X,Y,Z,dzdx) ;shading faceted;brighten(0.1);colorbar( 'horiz');title('No. 3 surf(X,Y,Z,dzdx)');subplot(1,2,2),surf(X,Y,Z,dzdy);shading faceted;colorbar( 'horiz');title('No. 4 surf(X,Y,Z,dzdy)');%色彩分别表现函数的 x 方向和 y 方向导数特征figure;subplot(1,2,1),surf(X,Y,Z,abs(dzdr)) ;shading faceted;brighten(0.6);colorbar( 'horiz');title('No. 5 surf(X,Y,Z,abs(dzdr))');subplot(1,2,2),surf(X,Y,Z,abs(dz2));shading faceted;colorbar( 'horiz');title('No. 6 surf(X,Y,Z,abs(dz2))'); | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | %% ======切片图和切片等位线图=======%利用 slice 和 contourslice 表现 MATLAB 提供的无限大水体中水下射流速度数据 flow 。 flow 是一组定义在三维空间上的函数数据。%在本例中,从图中的色标尺可知,深红色表示“正速度”(向图的左方),深蓝表示“负速度”(向图的右方)。% 以下指令用切面上的色彩表现射流速度[X,Y,Z,V]=flow; % 取 4个 的射流数据矩阵, V 是射流速度。x1=min(min(min(X)));x2=max(max(max(X))); % 取 x 坐标上下限y1=min(min(min(Y)));y2=max(max(max(Y))); % 取 y 坐标上下限z1=min(min(min(Z)));z2=max(max(max(Z))); % 取 z 坐标上下限sx=linspace(x1+1.2,x2,5); % 确定 5个垂直 x 轴的切面坐标sy=0; % 在 y=0处,取垂直 y 轴的切面sz=0; % 在 z=0处,取垂直 z 轴的切面figure;slice(X,Y,Z,V,sx,sy,sz); % 画切片图view([-12,30]);shading interp;colormap jet;axis off;colorbar;% 以下指令用等位线表现射流速度v1=min(min(min(V)));v2=max(max(max(V))); % 射流速度上下限cv=linspace(v1,v2,15); % 在射流上下限之间取 15条等位线figure;contourslice(X,Y,Z,V,sx,sy,sz,cv);view([-12,30]);colormap jet;colorbar;box on; | 
下面两段程序均不便上图,自己拿到Matlab里面运行一下看效果吧。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | %% =======动态图形=========%简单二维示例-彗星状轨迹图figure;n=10;t=n*pi*(0:0.0005:1);x=sin(t);y=cos(t);plot(x,y,'g');axis square;hold oncomet(x,y,0.01);hold off%卫星返回地球的运动轨线示意figure;R0=1; % 以地球半径为一个单位a=12*R0;b=9*R0;T0=2*pi; %T0 是轨道周期T=5*T0;dt=pi/100;t=[0:dt:T]';f=sqrt(a^2-b^2); % 地球与另一焦点的距离th=12.5*pi/180; % 卫星轨道与 x-y 平面的倾角E=exp(-t/20); % 轨道收缩率x=E.*(a*cos(t)-f);y=E.*(b*cos(th)*sin(t));z=E.*(b*sin(th)*sin(t));plot3(x,y,z,'g') % 画全程轨线[X,Y,Z]=sphere(30);X=R0*X;Y=R0*Y;Z=R0*Z; % 获得单位球坐标grid on,hold on,surf(X,Y,Z),shading interp % 画地球x1=-18*R0;x2=6*R0;y1=-12*R0;y2=12*R0;z1=-6*R0;z2=6*R0;axis([x1 x2 y1 y2 z1 z2]) % 确定坐标范围view([11737]),comet3(x,y,z,0.02),hold off % 设视角、画运动轨线%色彩变幻‘在 256色情况下,才可被正确执行.图片刷新可能会卡,单独执行spinmap可查看到效果figure;peaks;spinmap; | 
| 1 2 3 4 5 6 7 8 9 10 11 12 | %% =======影片动画 =======%三维图形的影片动画figure;shg,x=3*pi*(-1:0.05:1);y=x;[X,Y]=meshgrid(x,y);R=sqrt(X.^2+Y.^2)+eps; Z=sin(R)./R;h=surf(X,Y,Z);colormap(cool);axis offn=12;mmm=moviein(n); %预设画面矩阵。新版完全可以取消此指令 。fori=1:nrotate(h,[001],25); %是图形绕 z 轴旋转 25度 / 每次mmm(:,i)=getframe; %捕获画面。新版改为 mmm(i)=getframe 。endmovie(mmm,5,10) %以每秒10帧速度,重复播放5次 | 
- 招募大量matlab技术人员,有大量matlab需求订单,均为个人短期可以完成,有时间的朋友可以加我微信 :Ahxyz6666,加好友备注博客园matlab技术即可。
  作者:JeromeWang
  邮箱:yunfeiwang@hust.edu.cn
  出处:http://www.cnblogs.com/jeromeblog/
  本文版权归作者所有,欢迎转载,未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
                     
                    
                 
                    
                








 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号