matlab(绘图方法 画图 出图 函数) 精通matlab作图

更新时间:2024-04-26 23:27:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第 6 章 数据可视及探索

视觉是人们感受世界、认识自然的最重要依靠。数据可视化的目的:借助几何、色彩媒质表现一堆貌似杂乱的离散数据集合的形态,暴露数据内在关系和总体趋势,进而揭示出数据所传递的内在本质。随着计算机软硬件的发展,能力越来越强的图形表现,反过来对科学计算方法本身也产生了越来越大的影响。 针对符号计算和数值计算,MATLAB配置了两套形式上不同的绘图指令:“图形易绘指令”和“数值绘图指令”。前者配合符号计算,已安排在第5.8节;而本章内容将完全围绕数值绘图展开。 本章将系统地阐述:离散数据表示成图形的基本机理;曲线、曲面绘制的基本技法和指令;特殊图形的生成和使用示例;如何使用线型、色彩、数据点标记凸现不同数据的特征;如何利用着色、灯光照明、反射效果、材质体现和透明度处理渲染、烘托表现高维函数的性状;如何生成和运用标识,画龙点睛般地注释图形;如何表现变址、灰度、真彩图象;如何制作动画等。 随着MATLAB版本的升级,现今的MATLAB 图形窗不再是“单向性的图形显示工具”,而已成为进行“双向性探索的图形交互界面”。为此,占本章四分之一篇幅的第6.8节用于专门叙述全交互式绘图、图形对象属性的交互式设置、绘图用M函数文件的自动生成、以及用于数据探索的数据探针、数据刷和数据链。 整章内容安排遵循由浅入深、由基本到高级、由算例带归纳的原则。所有算例都是运行实例,易于读者实践试验,并从中掌握一般规律。 顺便指出:由于纸质印刷版无法表现图形色彩,因此,请读者阅读本章时,能同时参看对应的电子文档“ch06_数据可视及探索.doc”。该文档存放在随书光盘mbook目录。此外,算例中带exm前缀文件名的M文件电子文档则保存在随书光盘的mfile目录上。

6.1 引导

6.1.1

离散数据和离散函数的可视化

【例6.1-1】图形表示离散函数y? n 。(图6.1-1)

n=(-10:10)'; y=abs(n);

plot(n,y,'r.','MarkerSize',20) axis equal

grid on xlabel('n')

1

121086420-2-10-8-6-4-20n246810图 6.1-1 离散函数的可视化

6.1.2 连续函数的可视化

【例6.1-2】用图形表示连续调制波形y?sin(t)sin(9t)。(图6.1-2)

t1=(0:11)/11*pi; t2=(0:400)/400*pi; t3=(0:50)/50*pi; y1=sin(t1).*sin(9*t1); y2=sin(t2).*sin(9*t2); y3=sin(t3).*sin(9*t3);

subplot(2,2,1),plot(t1,y1,'r.') %<7> axis([0,pi,-1,1]),title('(1)点过少的离散图形') subplot(2,2,2),plot(t1,y1,t1,y1,'r.') %<9> axis([0,pi,-1,1]),title('(2)点过少的连续图形') subplot(2,2,3),plot(t2,y2,'r.') %<11> axis([0,pi,-1,1]),title('(3)点密集的离散图形') subplot(2,2,4),plot(t3,y3) %<13> axis([0,pi,-1,1]),title('(4)点足够的连续图形')

2

(1)点过少的离散图形10.50-0.5-1012310.50-0.5-10(2)点过少的连续图形123(3)点密集的离散图形10.50-0.5-1012310.50-0.5-10(4)点足够的连续图形123图 6.1-2 连续函数的图形表现方法

【例6.1-3】绘制奇数正多边形及圆。(图6.1-3)

N=9; t=0:2*pi/N:2*pi; x=sin(t);y=cos(t); tt=reshape(t,2,(N+1)/2); tt=flipud(tt); tt=tt(:); xx=sin(tt);yy=cos(tt);

subplot(1,2,1),plot(x,y)

title('(1) 正常排序图形'),axis equal off,shg subplot(1,2,2),plot(xx,yy)

title('(2) 非正常排序图形'),axis equal off,shg (1) 正常排序图形(2) 非正常排序图形图 6.1-3 自变量排列次序对连续曲线图形的影响

3

6.1.3 可视化的一般步骤 1. 2.

绘制二维图形的一般步骤 绘制三维图形的一般步骤

6.2

6.2.1 1.

二维线图及修饰操作

基本指令plot的调用格式 基本调用格式

【例6.2-1】本例演示:plot的最基本调用格式;绘图的基本步骤和方法;“三元组”的含义;plot的单输入调用格式,以及它所产生图形与“三元组”图形的区别。

x=0:0.05*pi:2*pi; y=exp(-x/3).*cos(2*x); subplot(2,1,1) plot(x,y,'o-r') axis([0,2*pi,-1,1]) subplot(2,1,2) plot(y) axis([1,length(y),-1,1])

图 6.2-1 三元组调用格式与单输入格式图形的区别

2. 衍生调用格式

【例6.2-2】本例演示:因变量为多列数组 的plot调用格式;plot(t,Y) plot(Y)所绘曲线的区别;“线宽”属性的设置。(图6.2-2)

4

clf t=(0:pi/20:2*pi)'; k=0.4:0.1:1; Y=cos(t)*k; plot(t,Y,'LineWidth',2) axis tight

图 6.2-2 采用矩阵因变量和默认色彩绘制多条曲线

3. 带属性设置的调用格式

【例6.2-3】用图形表示连续调制波形y?sin(t)sin(9t) 及其包络线。(图6.2-3)

t=(0:pi/100:pi)'; %<1> y1=sin(t)*[1,-1]; %<2> y2=sin(t).*sin(9*t); %<3>

t3=pi*(0:9)/9; % <4> y3=sin(t3).*sin(9*t3); % <5> plot(t,y1,'r:',t,y2,'-bo') % <6> hold on

plot(t3,y3,'s','MarkerSize',10,'MarkerEdgeColor',[0,1,0],'MarkerFaceColor',[1,0.8,0]) % <8> axis tight %<9> hold off %<10> % %

%plot(t,y1,'r:',t,y2,'-bo',t3,y3,'s','MarkerSize',10,'MarkerEdgeColor',[0,1,0],'MarkerFaceColor',[1,0.8,0]) % <11>

5

10.80.60.40.20-0.2-0.4-0.6-0.8-100.511.522.53图6.2-3 采用属性设置所绘的曲线

6.2.2 1.

坐标控制和图形标识 坐标轴的控制

【例6.2-4】观察各种轴控制指令的影响。演示采用长轴为3.25,短轴为1.15的椭圆。注意:采用多子图(图6.2-4)表现时,图形形状不仅受“控制指令”影响,而且受整个图面“宽高比”及“子图数目”的影响。本书这样处理,是出于篇幅考虑。读者欲想准确体会控制指令的影响,请在全图状态下进行观察。

t=0:2*pi/99:2*pi;

x=1.15*cos(t);y=3.25*sin(t); subplot(2,3,1) plot(x,y)

axis normal grid on title('Normal and Grid on') subplot(2,3,2),plot(x,y)

axis equal grid on,title('Equal') subplot(2,3,3),plot(x,y)

axis square grid on,title('Square') subplot(2,3,4),plot(x,y)

axis image box off title('Image and Box off') subplot(2,3,5),plot(x,y)

axis image fill box off,title('Image and Fill') subplot(2,3,6),plot(x,y)

axis tight box off,title('Tight')

6

Normal and Grid on420-2-4-20Image and Box off20-2-10110.50-0.5-1-101-2-1010220-2-202Equal420-2-4-20Tight22SquareImage and Fill图6.2-4 各种轴控制指令的不同影响 2. 3.

分格线和坐标框 图形标识指令 标识字符的精细控制

4.

【例6.2-5】通过绘制二阶系统阶跃响应。,综合演示图形标识。本例比较综合,涉及的指令较广。请读者耐心读、实际做、再看例后说明,定会有匪浅收益。(图6.2-5 )

clf;t=6*pi*(0:100)/100;

y=1-exp(-0.3*t).*cos(0.7*t);

plot(t,y,'r-','LineWidth',3) %<3> hold on

tt=t(find(abs(y-1)>0.05));ts=max(tt); %<5> plot(ts,0.95,'bo','MarkerSize',10) %<6> hold off axis([-inf,6*pi,0.6,inf])

set(gca,'Xtick',[2*pi,4*pi,6*pi],'Ytick',[0.95,1,1.05,max(y)])%<9> set(gca,'XtickLabel',{'2*pi';'4*pi';'6*pi'}) %<10> set(gca,'YtickLabel',{'0.95';'1';'1.05';'max(y)'})%<11> grid on

text(13.5,1.2,'\\fontsize{12}{\\alpha}=0.3') %<13> text(13.5,1.1,'\\fontsize{12}{\\omega}=0.7') %<14> cell_string{1}='\\fontsize{12}\%uparrow'; %<15>

cell_string{2}='\\fontsize{16} \\fontname{隶书}镇定时间'; cell_string{3}='\\fontsize{6} '; cell_string{4}=['\\fontsize{14}\\rmt_{s} = ' num2str(ts)]; %<18> text(ts,0.85,cell_string,'Color','b','HorizontalAlignment','Center') %<19> title('\\fontsize{14}\\it y = 1 - e^{ -\\alpha t}cos{\\omegat}') %<20> xlabel('\\fontsize{14} \\bft \\rightarrow')

7

ylabel('\\fontsize{14} \\bfy \\rightarrow') %<22> y = 1 - e -? tcos?tmax(y)?=0.3?=0.71.051 y ?0.95? 镇定时间ts = 9.6133 2*pi4*pi6*pi t ?图 6.2-5 二阶阶跃响应图的标识

6.2.3 1.

多次叠绘、双纵坐标和多子图 多次叠绘

【例6.2-6】利用hold绘制离散信号通过零阶保持器后产生的波形。(图5.2-6)

t=2*pi*(0:20)/20;

y=cos(t).*exp(-0.4*t);

stem(t,y,'g','Color','k'); hold on

stairs(t,y,':r','LineWidth',3) hold off

legend('\\fontsize{14}\\it stem','\\fontsize{14}\\it stairs') box on

8

1 0.8 stem stairs0.60.40.20-0.2-0.4 01234567图 6.2-6 两类不同曲线绘于同一图布 2.

双纵坐标图

【例6.2-7】画出函数y?xsinx和积分s??x0(图6.2-7)。 (xsinx)dx在区间[0,4]上的曲线

clf;dx=0.1;x=0:dx:4;y=x.*sin(x);

s=cumtrapz(y)*dx; %<2>

a=plotyy(x,y,x,s,'stem','plot'); text(0.5,1.5,'\\fontsize{14}\\ity=xsinx') %<4>

sint='{\\fontsize{16}\\int_{\\fontsize{8}0}^{ x}}'; ss=['\\fontsize{14}\\its=',sint,'\\fontsize{14}\\itxsinxdx']; text(2.5,3.5,ss) %<7>

set(get(a(1),'Ylabel'),'String','被积函数 \\ity=xsinx') set(get(a(2),'Ylabel'),'String',ss) xlabel('x')

%<3> %<5> %<6> %<8> %<9>

9

54s=? xxsinxdx0y=xsinx02被积函数 y=xsinx-500.511.52x2.533.504s=? xxsinxdx0图 6.2-7 函数和积分

3. 多子图

【例6.2-8】演示subplot指令对图形窗的分割(图6.2-8)。

clf;t=(pi*(0:1000)/1000)';

y1=sin(t);y2=sin(10*t);y12=sin(t).*sin(10*t); subplot(2,2,1),plot(t,y1);axis([0,pi,-1,1]) subplot(2,2,2),plot(t,y2);axis([0,pi,-1,1]) subplot('Position',[0.2,0.1,0.6,0.40]) plot(t,y12,'b-',t,[y1,-y1],'r:') axis([0,pi,-1,1])

10

10.50-0.5-110.50-0.5-1010.50-0.5-1123012300.511.522.53图 6.2-8 多子图的布置 6.3 三维绘图及修饰操作

6.3.1 三维线图指令plot3

【例6.3-1】本例演示:三维线图的“四元组”调用格式;表现线函数的参数方程。

t=(0:0.02:2)*pi; x=sin(t);y=cos(t);z=cos(2*t); plot3(x,y,z,'b-',x,y,z,'bd') view([-82,58]) box on legend('链','宝石','Location','Best')

11

链宝石110.50.50-0.5-11 0.50-0.5-1-0.50-1图 6.3-1 宝石项链

6.3.2 1.

三维曲面/网线图指令 基本调用格式

22【例6.3-2】用曲面图表现函数z?x?y(图6.3-2)。

clf

x=-4:4; y=x; [X,Y]=meshgrid(x,y); Z=X.^2+Y.^2; surf(X,Y,Z); colormap(hot) %<7> hold on stem3(X,Y,Z,'bo') hold off

xlabel('x'),ylabel('y'),zlabel('z') axis([-5,5,-5,5,0,inf]) view([-84,21])

12

302520z15105050y图 6.3-2 曲面图和格点 -5-5x50 2. 3. 4.

衍生调用格式 色图colormap 浓淡处理shading

【例6.3-3】三种浓淡处理方式比较。(图6.3-3)

clf x=-4:4;y=x;

[X,Y]=meshgrid(x,y); Z=X.^2+Y.^2; surf(X,Y,Z) colormap(jet)

subplot(1,3,1),surf(Z),axis off

subplot(1,3,2),surf(Z),axis off,shading flat subplot(1,3,3),surf(Z),axis off,shading interp set(gcf,'Color','w')

图 6.3-3 三种浓淡处理方式faceted/flat/interp的效果比较

6.3.3 视点控制和图形的旋动

13

1. 视点控制view

图 6.3-4 视点设置参数示意

2. 图形旋动rotate

图 6.3-5 旋转图形对象的右手螺旋法则

【例 6.3-4】本例演示:surf指令的四元组调用法;用colorbar色轴标注数值大小;colorbar所产生色轴的位置控制;旋转指令rotate的调用格式。(图6.3-6)

clf;[X,Y] = meshgrid([-2:.2:2]); Z=4*X.*exp(-X.^2-Y.^2); G=gradient(Z); subplot(1,2,1),surf(X,Y,Z,G)

colorbar('Location','North','Position',[0.37,0.90,0.295,0.015]) subplot(1,2,2),h=surf(X,Y,Z,G); rotate(h,[-2,-2,0],30,[2,2,0]), colormap(jet)

14

图 6.3-6 图形对象的旋转

6.3.4 1. 2.

光照、材质和透视 光照light 材质处理material

【例6.3-5】灯光、照明、材质指令所表现的图形。(图6.3-7)

clf;

[X,Y,Z]=sphere(40); colormap(jet) subplot(1,2,1),surf(X,Y,Z),axis equal off,shading interp light ('position',[0 -10 1.5],'style','infinite') lighting phong material shiny subplot(1,2,2),surf(X,Y,Z,-Z),axis equal off,shading flat light;lighting flat light('position',[-1,-1,-2],'color','y') light('position',[-1,0.5,1],'style','local','color','w') set(gcf,'Color','w')

%<3> %<4> %<5> %<6> %<7> %<8> %<9> %<10> %<11>

图 6.3-7 灯光、照明、材质指令所表现的图形

3.

透明处理

15

【例6.3-6】本例演示:alpha, alim, alphamap的多种调用格式及其它们的配用。 (1)

figure(close) [X,Y,Z]=peaks(20);

surf(X,Y,Z);xlabel('x'),ylabel('y'),zlabel('z') shading flat

alpha(0.5) %<5>

图 6.3-8 透明度相同的曲面

(2)

alpha('x')

%<6>

16

图 6.3-9 随x坐标增加而变得愈不透明

(3)

alpha(Z) alim([-3,3]) alpha('scaled') alpha('interp')

%<7> %<8> %<9> %<10>

图 6.3-10 透明轴设定并采用插补产生透明度

(4)

shading interp alpha(Z) alpha('interp') alphamap('vdown')

17

图 6.3-11 中部最透明而上下端最不透明的曲面

(5)

Alimit=alim alim_mode=alim('mode')

Alimit =

-3 3 alim_mode = manual

6.3.5 1.

消隐、镂空和裁切 网线的消隐

【例6.3-7】透视演示(图6.3-12)。

[X0,Y0,Z0]=sphere(30); X=2*X0;Y=2*Y0;Z=2*Z0; surf(X0,Y0,Z0); shading interp hold on,mesh(X,Y,Z),colormap(hot) hold off

hidden off axis equal,axis off

18

图6.3-12 剔透玲珑球

2. 图形的镂空

【例6.3-8】演示:如何利用“非数”NaN,对图形进行镂空处理。(图6.3-13)

P=peaks(30); P(18:20,9:15)=NaN; surfc(P);colormap(summer)

light('position',[50,-10,5]),lighting flat material([0.9,0.9,0.6,15,0.4])%材质控制

图6.3-13 镂方孔的曲面

3. 裁切

【例6.3-9】表现切面(图6.3-14)。

19

clf,x=[-8:0.1:8];y=x;[X,Y]=meshgrid(x,y);ZZ=X.^2-Y.^2; ii=find(abs(X)>6|abs(Y)>6); ZZ(ii)=zeros(size(ii));

surf(X,Y,ZZ),shading interp;colormap(copper) light('position',[0,-15,1]);lighting phong material([0.8,0.8,0.5,10,0.5])

图6.3-14 经裁切处理后的图形

6.4 高维可视化

6.4.1

二维半图线

【例6.4-1】本例演示:pcolor , contour, contourf的调用格式;等位线标高指令 clabel 的配合使用和区别;colorbar在用户指定位置上创建着色标尺;subplot子图位置的控制;图形窗背景底色的设置。(图6.4-1)

clf;clear

[X,Y,Z]=peaks(30); n=6;

subplot('Position',[0.13,0.11,0.335,0.75]) pcolor(X,Y,Z) shading interp

zmax=max(max(Z)); zmin=min(min(Z)); caxis([zmin,zmax])

colorbar('Location','North','Position',[0.25,0.92,0.515,0.025]) hold on

C=contour(X,Y,Z,n,'k:'); clabel(C) hold off

subplot('Position',[0.57,0.11,0.335,0.75]) [C,h]=contourf(X,Y,Z,n,'k:'); clabel(C,h) colormap(cool) set(gcf,'Color','w')

20

3.895.96-0.237-2.31.831.83-4.37-2.3-0.237图 6.4-1 “二维半”指令的演示

6.4.2 准四维表现

【例6.4-2】用颜色表现z?f(x,y)函数的梯度、曲率等特征。(图6.4-2)

clf

x=3*pi*(-1:1/15:1);y=x;[X,Y]=meshgrid(x,y); R=sqrt(X.^2+Y.^2);

R(R==0)=eps; Z=sin(R)./R;

[dzdx,dzdy]=gradient(Z); %<5> dzdr=sqrt(dzdx.^2+dzdy.^2); %<6>

subplot('Position',[0.13,0.21,0.335,0.65]) % <7> surf(X,Y,Z,abs(dzdr)) %<8> shading interp;

colorbar('Location','North','Position',[0.25,0.12,0.515,0.025]) %<10> brighten(0.6); colormap jet

alphamap('rampup')

alpha('color') %<14> alpha('interp')

title('No. 1 surf(X,Y,Z,abs(dzdr))') dz2=del2(Z); %<17>

subplot('Position',[0.57,0.21,0.335,0.65]) % <18> surf(X,Y,Z,abs(dz2)) %<19> shading interp

title('No. 2 surf(X,Y,Z,abs(dz2))')

21

图 6.4-2 准四维表现曲面的径向导数和曲率特征

6.4.3 四维切片及等位线

【例6.4-3】利用slice和contourslice 表现MATLAB提供的无限大水体中水下射流速度数据flow 。flow是一组定义在三维空间上的函数数据。本例将动用切片、视角、色彩和透明度等综合手段观察定义在三维空间上的函数。 (1)

figure(1);clf

[X,Y,Z,V]=flow;

x1=min(min(min(X)));x2=max(max(max(X))); y1=min(min(min(Y)));y2=max(max(max(Y))); z1=min(min(min(Z)));z2=max(max(max(Z))); sx=linspace(x1+1.2,x2,5); sy=0; sz=0;

slice(X,Y,Z,V,sx,sy,sz); view([-33,36]);shading interp; colormap jet;

alpha('color') alphamap('rampdown') colorbar axis off u=caxis;

22

图 6.4-3 切片图

(2)

figure(2);clf

v1=min(min(min(V)));v2=max(max(max(V))); cv=linspace(v1,v2,15); contourslice(X,Y,Z,V,sx,sy,sz,cv) view([-12,30]) colormap jet colorbar

caxis(u) box on

23

图 6.4-4 切片等位线图

6.5

6.5.1 1.

动态图形

高层指令生成动态图形 彗星状轨迹图

【例6.5-1】卫星返回地球的运动轨线示意。(请读者自己在指令窗中运行以下指令。)

shg;R0=1; a=12*R0;b=9*R0; T0=2*pi; T=5*T0;dt=pi/100;t=[0:dt:T]'; f=sqrt(a^2-b^2); th=12.5*pi/180; 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([133 65]) comet3(x,y,z,0.02) hold off

24

图 6.5-1 卫星返回地球轨线示意

2. 色图的变幻

【例 6.5-2】指令spinmap的应用。(图6.5-2)

ezsurf('x*y','circ');shading flat;view([-18,28]) C=summer; CC=[C;flipud( C )]; colormap(CC) spinmap(30,4)

图 6.5-2 用于色图变幻演示的图形

3. 影片动画

【例6.5-3】三维图形的影片动画。因印刷关系,在纸面上无法表现,请读者自己在指令窗中运行以下指令。 (1)

clf

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(jet);axis off n=12;

25

for i=1:n rotate(h,[0 0 1],25); mmm(i)=getframe; end close

(2)

shg,axis off

movie(mmm,5,10)

6.5.2 低层指令生成实时动画

【例 6.5-4】制作红色小球沿一条带封闭路径的下旋螺线运动的实时动画(图6.5-3)。 (1)

function f=exm060504_anim(K,ki)

t1=(0:1000)/1000*10*pi;x1=cos(t1);y1=sin(t1);z1=-t1;

t2=(0:10)/10;x2=x1(end)*(1-t2);y2=y1(end)*(1-t2);z2=z1(end)*ones(size(x2)); t3=t2;z3=(1-t3)*z1(end);x3=zeros(size(z3));y3=x3; t4=t2;x4=t4;y4=zeros(size(x4));z4=y4; x=[x1 x2 x3 x4]; n=length(x);

if nargin<2 ki=fix(n/2); end

y=[y1 y2 y3 y4];z=[z1 z2 z3 z4]; shg

plot3(x,y,z,'Color',[1,0.6,0.4],'LineWidth',2.5) axis off

h=line('xdata',x(1),'ydata',y(1),'zdata',z(1),'Color','r','Marker', '.', 'MarkerSize',40,'EraseMode','xor'); KK=K*n;

text(-1,-0.85,-36,'倒计数') KK=KK-1;

htext=text(-1,-1,-40,int2str(KK)); i=2;j=1;

while 1 set(h,'xdata',x(i),'ydata',y(i),'zdata',z(i));

drawnow; % <23> pause(0.0005) % <24> i=i+1;

KK=KK-1;

set(htext,'string',int2str(KK)) %<27> if nargin==2 && nargout==1

if(i==ki&&j==1);f=getframe(gcf);end % <29> end if i>n

i=1;j=j+1;

if j>K;break;end end end

(2)

shg

f=exm060504_anim(1,254);

26

(3)

image(f.cdata),axis off

图6.5-3 红球沿下旋螺线运动的瞬间照片

6.6 特殊图形指令

6.6.1 1.

彩色份额图 面域图area

【例6.6-1】面域图指令area的用法 。(图6.6-1)

x=-2:2

Y=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]%(3*5)的Y数组的 CS=flipud(cumsum(Y)) area(x',Y',0)

legend('因素A','因素B','因素C') grid on,colormap(spring) x =

-2 -1 0 1 2 Y =

3 5 2 4 1 3 4 5 2 1 5 4 3 2 5 CS =

11 13 10 8 7 6 9 7 6 2 3 5 2 4 1

27

14121086420 -2因素A因素B因素C -1.5-1-0.500.511.52图 6.6-1 面域图表现各分量的贡献

2. 直方图bar, barh, bar3, bar3h

【例6.6-2】本例演示:二维、三维直方图绘制指令bar和bar3的基本用法(图6.6-2)。每种直方图都有两种图型:垂直直方图(bar, bar3)和水平直方图(barh, bar3h)。而每种图型又有两种表现模式:累计式(对应关键词为stacked):分组式(对应关键词为grouped)。

x=-2:2; Y=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; subplot(1,2,1)

bar(x',Y','stacked')

xlabel('x'),ylabel('\\Sigma y'),colormap(cool) legend('因素A','因素B','因素C') subplot(1,2,2)

bar3h(x',Y','grouped') 1412108因素A因素B因素C 210-16-2420 024-2-10x12y6? yx图 6.6-2 二维、三维直方图

3. 饼图pie, pie3

【例6.6-3】饼图指令pie , pie3 用来表示各元素占总和的百分数(图6.6-3)。该指令第二输入宗量是与第一宗量同长的0-1向量,1使对应扇块突出。

a=[1,1.6,1.2,0.8,2.1];

28

subplot(1,2,1)

pie(a,[1 0 1 0 0]) axis equal

legend({'1','2','3','4','5'},'Location','EastOutside') subplot(1,2,2)

pie3(a,double(a==min(a))) colormap(cool) 151$% 1234515$1%图 6.6-3 饼形统计图

6.6.2 有向线图

【例6.6-4】 本例演示:compass和feather指令要求输入量是“直角坐标系”数据;把极坐标变换为直角坐标的指令pol2cart。(图6.6-4)

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')

Feather1Compass90 1120 0.5600.80.6300.40.218000-0.2210330-0.4-0.6-0.8-1150240270300051015图 6.6-4 compass和feather指令的区别 6.6.3 多面体异形图

29

1. 德洛奈三角剖分和Voronoi图

【例6.6-5】本例演示:德洛奈三角剖分(Delaunay Triangulation)指令delaunay和Voronoi多边形(Voronoi Polygon)指令voronoi的调用格式;fill采用色图色彩填色;借助图柄设置线宽属性。(图6.6-5)

clf;

rng(111,'v5uniform') n=30; X=rand(n,1)-0.5; Y=rand(n,1)-0.5; T=delaunay(X,Y); Tc=[T T(:,1)]; hold on;axis square ism=1:length(T); fh=fill(X(Tc'),Y(Tc'),ism); set(fh([28,37]),'LineWidth',5) voronoi(X,Y,'r') colormap(summer) hold off 0.50.40.30.20.10-0.1-0.2-0.3-0.4-0.50图 6.6-5 Delaubay三角剖分和Voronoi多边形 0.5

2. 填色图fill,fill3

【例6.6-6】本例演示:表示fill指令所填色多边形的数组必须首尾数据重合(见第<4>行);fill或fill3输入量构造的多边形都必须是封闭的(见第<9><10><11>行);任意位置图例的生成(见第<14><15>行)。(图6.6-6)

clf

subplot('Position',[0.10,0.16,0.205,0.75]) n=10;dt=2*pi/n;t=0:dt:2*pi; t=[t,t(1)]; %<4>

30

x=sin(t);y=cos(t); fill(x,y,'c') axis equal off

subplot('Position',[0.44,0.16,0.335,0.75]) X=[0.5 0.5 0.5 0.5;0.5 0.5 0.5 0.5;0 1 1 0]; Y=[0.5 0.5 0.5 0.5;0.5 0.5 0.5 0.5;0 0 1 1]; Z=[1 1 1 1;0 0 0 0;0 0 0 0]; %<11> C=[1,10,20,30]; fill3(X,Y,Z,C)

LH=legend('1','2','3','4','Location','SouthWest'); set(LH,'Position',[0.291,0.122,0.121,0.136]) view([-16 74]),colormap cool

xlabel('x'),ylabel('y'),box on;grid on % % <9> <10>

%<14>

10.5010.80.60.412340.20.500x1y 图 6.6-6 用fiil和fill3对多边形填色

3.

不规则数据的网线图和曲面图

sinR,R?x2?y2。(图6.6-7) R【例6.6-7】用三角网线、曲面图表现函数z?rng(10)

X=6*pi*(rand(120,1)-0.5);

Y=6*pi*(rand(120,1)-0.5); R=sqrt(X.^2+Y.^2);

R(R==0)=eps;Z=sin(R)./R;

tri=delaunay(X,Y); subplot(1,2,1),trimesh(tri,X,Y,Z) subplot(1,2,2),trisurf(tri,X,Y,Z) colormap(jet);brighten(0.5)

31

10.50-0.510100-10-10010.50-0.510100-10-10图 6.6-7 不规则数据的三维表现

0 4. 彩带图ribbon

1在不同?值时的2s?2?s?1%<2>

【例6.6-8】用彩带绘图指令ribbon ,绘制归化二阶系统G?阶跃响应(见图6.6-8)。

clear

zeta2=[0.1 0.2 0.3 0.4 0.5 0.6 0.8 1.0]; n=length(zeta2);

for k=1:n;Num{k,1}=1;Den{k,1}=[1 2*zeta2(k) 1];end S=tf(Num,Den); t=(0:0.4:30)'; [Y,~]=step(S,t); tt=t*ones(size(zeta2)); ribbon(tt,Y,0.4)

view([150,50]),shading interp,colormap(jet) light,lighting phong,box on for k=1:n;str_lgd{k,1}=num2str(zeta2(k));end legend(str_lgd)

str1='\\itG = (s^{2} + 2\\zetas + 1)^{-1}'; str2='\\fontsize{14}\\fontname{隶书}取不同'; str3='{\\fontsize{10}\\it \\zeta }';

str4='\\fontsize{14}\\fontname{隶书}时的阶跃响应'; title([str1,str2,str3,str4]) ylabel('t')

zlabel('\\ity(\\zeta,t) \\rightarrow')

32

图 6.6-8 二阶系统在不同阻尼系数时的响应

6.6.4 散点图scatter和plotmatrix

【例6.6-9】模拟性地表现受噪声污染的64QAM 星座信号。(图6.6-9)

rng(2)

S=randsrc(10000,2,[-7,-5,-3,-1,1,3,5,7]); Sn=S+0.3*randn(size(S)); scatter(Sn(:,1),Sn(:,2),'.') grid on,box on,axis equal axis([-9,9,-9,9])

xlabel('实部'),ylabel('虚部')

33

图 6.6-9 含噪声64QAM信号的二维散点图

【例6.6-10】本例演示:plotmatrix的三种调用格式;(p?n)的X数组和(p?m)的Y数组,在plotmatrix(X,Y)作用下,画出(m?n)个小子图,其中第(i,j)个小子图是根据Y第i列和X第j列数据画出的;plotmatrix(X)作用与plotmatrix(X,X)相同;由同列相互作用产生的小子图位置画出频数直方图。(图6.6-10)

rng default

X=randn(1000,2); Y=rand(1000,1); subplot(1,3,1),plotmatrix(X) subplot(1,3,2),plotmatrix(Y) subplot(1,3,3),plotmatrix(X,Y)

图 6.6-10 plotmatrix表现数据统计特性

6.6.5 泛函绘图指令fplot

【例6.6-11】本例演示:fplot 与ezplot, plot等线图指令的绘图效果比较。(图6.6-11)

fun=@(t)(sin(1./t)); subplot(1,3,1),ezplot(fun,[0.01,0.1])

title('\\fontsize{10}\\fontname{隶书}ezplot绘图效果')

34

t=linspace(0.01,0.1,50);

subplot(1,3,2),plot(t,fun(t)) axis([0.01,0.1,-1.23,1.23]),xlabel('t')

title('\\fontsize{10}\\fontname{隶书}plot绘图效果') subplot(1,3,3),fplot(fun,[0.01,0.1],1e-3) axis([0.01,0.1,-1.23,1.23]),xlabel('t')

title('\\fontsize{10}\\fontname{隶书}fplot绘图效果') ezplot绘图效果11plot绘图效果1fplot绘图效果0.50.50.5000-0.5-0.5-0.5-10.05t0.1-1-10.05t0.10.050.1t 图6.6-11 fplot自适应绘图的优点体现

6.7 图象

6.7.1 图象的类别和显示 6.7.2 图象的读写

【例6.7-1】图象文件的读取和图象的显示。 (1)

clear

im1=imfinfo('trees','tif');

disp(['原图像文件格式',blanks(5),'图像类型'])

disp([im1(1).Format,blanks(14),im1(1).ColorType]) [X,cmap]=imread('trees.tif'); image(X);colormap(cmap) axis image off title('变址图像')

原图像文件格式 图像类型 tif indexed

35

变址图像图 6.7-1 变址图象

(2)

imbody=imfinfo('liftingbody','png');

disp(['原图像文件格式',blanks(5),'图像类型'])

disp([imbody.Format,blanks(14),imbody.ColorType]) X=imread('liftingbody.png'); imagesc(X);colormap(gray) axis image off title('灰度图象')

原图像文件格式 图像类型 png grayscale

灰度图象图 6.7-2 灰度图象

(3)

imold=imfinfo('office_4','jpg');

disp(['原图像文件格式',blanks(5),'图像类型'])

disp([imold.Format,blanks(14),imold.ColorType]) X=imread('office_4.jpg'); imwrite(X,'ffzzy.tiff')

36

image(imread('ffzzy.tiff')) axis image off title('真彩图像')

原图像文件格式 图像类型 jpg truecolor

真彩图像图 6.7-3 真彩图象

6.8 图形窗的编辑探索功能

6.8.1 图形窗的结构 1.

图形窗的功能分区

37

图6.8-1 编辑状态的图形窗

2. 图形窗工具条

图 6.8-2 图形窗工具条专用按键

3. 主要构件与对应菜单

38

图 6.8-3 子图坐标选配窗的功能分区

39

图 6.8-4 图形对象浏览列表的功能

图 6.8-5 图形对象属性编辑器

6.8.2 指令鼠标混合操作生成绘图文件

【例6.8-1】本例演示:简单线图如何在图形窗编辑下转变成图6.8-6所示的图形;图形窗的属性编辑功能;如何从图形窗产生绘图函数文件;如何修改自动生成文件。

40

本文来源:https://www.bwwdw.com/article/rbup.html

Top