47
Edit by niuben Edit by niuben 2012.10.29 Matlab 绘绘

Matlab 绘图

  • Upload
    kaemon

  • View
    152

  • Download
    0

Embed Size (px)

DESCRIPTION

Matlab 绘图. MCM. 2012.10.29. 主要内容:. 1. 2. 3. 4. 5. 6. 引出:. 观察这组数据( 500 个),看看有什么规律?. 描点看规律??. 应用 MATLAB 对 500 组数据作出统计规律图. 可以看出这是一组正态分布的数据。都有哪些手段可以让数据的规律或性质更直接的展示出来呢?. 一、 Matlab 绘图问题. 二维图形. 散点、折线、一般曲线、极坐 标、柱状统计图、饼图、等高 线、向量图、图形修饰等等。. One. 三维图形. 曲线、曲面、柱面、散点、 饼图等等. Two. - PowerPoint PPT Presentation

Citation preview

Page 1: Matlab  绘图

Edit by niubenEdit by niuben

2012.10.29

Matlab 绘图Matlab 绘图

Page 2: Matlab  绘图

第第 22 页,共页,共 122122 页页 Edit by niubenEdit by niuben

主要内容:主要内容:

11

22

33

44

55

66

Page 3: Matlab  绘图

第第 33 页,共页,共 122122 页页 Edit by niubenEdit by niuben

引出:引出: 观察这组数据( 500 个),看看有什么规律?

Page 4: Matlab  绘图

第第 44 页,共页,共 122122 页页 Edit by niubenEdit by niuben

描点看规律??

Page 5: Matlab  绘图

第第 55 页,共页,共 122122 页页 Edit by niubenEdit by niuben

应用 MATLAB 对 500 组数据作出统计规律图应用 MATLAB 对 500 组数据作出统计规律图

可以看出这是一组正态分布的数据。都有哪些手段可以让数据的规律或性质更直接的展示出来呢?

Page 6: Matlab  绘图

第第 66 页,共页,共 122122 页页 Edit by niubenEdit by niuben

一、 Matlab 绘图问题

二维图形 OneOne散点、折线、一般曲线、极坐标、柱状统计图、饼图、等高线、向量图、图形修饰等等。

三维图形 TwoTwo曲线、曲面、柱面、散点、饼图等等

Page 7: Matlab  绘图

第第 77 页,共页,共 122122 页页 Edit by niubenEdit by niuben

(一)、二维图形(一)、二维图形

1.1. 数据准备数据准备 ** (必要步骤)(必要步骤)2.2. 设置绘图区设置绘图区3.3. 绘图绘图 **4.4. 设置曲线和记点格式设置曲线和记点格式5.5. 设置坐标轴和网格线属性设置坐标轴和网格线属性6.6. 标注图形标注图形7.7. 保存和导出图形保存和导出图形

Matlab 二维绘图的基本流程Matlab 二维绘图的基本流程

Page 8: Matlab  绘图

第第 88 页,共页,共 122122 页页 Edit by niubenEdit by niuben

二维图形二维图形

基本绘图函数基本绘图函数

画线函数: line

核心绘图函数: plot

极坐标绘图: polar

Page 9: Matlab  绘图

第第 99 页,共页,共 122122 页页 Edit by niubenEdit by niuben

1. line 函数绘图 *( 了解,下同 )1. line 函数绘图 *( 了解,下同 )

%……………………x=0:0.4*pi:2*pi;y=sin(x);line(x,y)%……………………%line(x,y) 将( x(i),y(i) )% 代表的点用线段依次连起

% 本书部分例题选自《精通 Matlab7 》

Page 10: Matlab  绘图

第第 1010 页,共页,共 122122 页页 Edit by niubenEdit by niuben

2.plot 函数绘图语法2.plot 函数绘图语法

plot 函数是 Matlab 中最核心的二维绘图函数,它有多种语法格式,可以实现很多功能。例如: plot ( y ); plot ( x , y )( y 可以是多维数组); plot ( x1 , y1 , x2 ,y2…… )等等。

Page 11: Matlab  绘图

第第 1111 页,共页,共 122122 页页 Edit by niubenEdit by niuben

例: plot 函数绘图例: plot 函数绘图

%……………………

x=0:0.4*pi:2*pi;y1=sin(x);y2=cos(x);y3=sin(x-0.1*pi);y4=cos(x+0.1*pi);plot(y1) %1plot(x,y1) %2plot(x,[y1;y2;y3;y4]) %3plot(x,y1,x,y2,x,y3,x,y4) %4%……………………

Page 12: Matlab  绘图

第第 1212 页,共页,共 122122 页页 Edit by niubenEdit by niuben

help plot (Matlab 帮助 )help plot (Matlab 帮助 )

Page 13: Matlab  绘图

第第 1313 页,共页,共 122122 页页 Edit by niubenEdit by niuben

例 . plot 绘图的用处

观察 sin(1/x) 在区间【 0.01 , 1 】上的零点个数。( sin(1/x) =0 的根)

x=0.001:.01:1; y=sin(1./x); plot(x,y);axis([0 1 -.3 .3]);

Page 14: Matlab  绘图

第第 1414 页,共页,共 122122 页页 Edit by niubenEdit by niuben

3. 例:极坐标绘图 *

polar 函数格式: polar(theta,rho)例:%……………………theta=0:0.05*pi:2*pi;r1=sin(theta);r2=cos(theta);polar([theta' theta'],[r1' r2'])%……………………

思考:此处 2*pi 若改为 pi 或 0.5*pi 或 0.25pi ,结果如何 ?

Page 15: Matlab  绘图

第第 1515 页,共页,共 122122 页页 Edit by niubenEdit by niuben

4.4. 曲线格式和标记点格式曲线格式和标记点格式

- 实线 r 红色 y 黄色-. 点划线 g 绿色 c 蓝绿色-- 虚线 b 蓝色 m 洋红色: 点线 k 黑色 w 白色

- 实线 r 红色 y 黄色-. 点划线 g 绿色 c 蓝绿色-- 虚线 b 蓝色 m 洋红色: 点线 k 黑色 w 白色

曲线线形 颜色曲线线形 颜色

+ 、 o 、 * 、 . 、 X 、 s( 方格 ) 、 d (菱形)、 ^ (上三角)、 v (下三角)、 < (左三角 )、 > (右三角)、 p (五边形)、 h (六边形)。

+ 、 o 、 * 、 . 、 X 、 s( 方格 ) 、 d (菱形)、 ^ (上三角)、 v (下三角)、 < (左三角 )、 > (右三角)、 p (五边形)、 h (六边形)。

标记点格式标记点格式

Page 16: Matlab  绘图

第第 1616 页,共页,共 122122 页页 Edit by niubenEdit by niuben

例: plot 函数绘图续 (线型、颜色)例: plot 函数绘图续 (线型、颜色)

%……………………x=0:0.1*pi:2*pi;y3=sin(x).*cos(3*x);y1=sin(x);y2=cos(3*x);plot(x,y1,'ob',x,y2,'--dc',x,y3,':vr')% 第一组数据只标记数据点%……………………

仅描点 --dc( 虚线菱形蓝绿 )

:vr (点、下三角红色)

Page 17: Matlab  绘图

第第 1717 页,共页,共 122122 页页 Edit by niubenEdit by niuben

5. 曲线格式(线宽、标记点大小,边框、颜色)*5. 曲线格式(线宽、标记点大小,边框、颜色)*

格式:plot(… ,’ PropertyName’,PropertyValue)

PropertyName

LineWidth

MarkerEdgeColor

MarkerFaceColor

MarkerSize

意义

线宽

标记点边框颜色

标记点填充颜色

标记点大小

选项

数值

颜色字符,’ r’……

同上

数值

Page 18: Matlab  绘图

第第 1818 页,共页,共 122122 页页 Edit by niubenEdit by niuben

6. 例 * :线宽、标记点大小,边框、颜色6. 例 * :线宽、标记点大小,边框、颜色

%……………………x=-5:0.5:5;y=5.*exp(-abs(x)).*sin(x);plot(x,y,'--hr','LineWidth',1.5,...'MarkerEdgeColor','b',...'MarkerFaceColor','m',...'MarkerSize',10)%……………………

Page 19: Matlab  绘图

第第 1919 页,共页,共 122122 页页 Edit by niubenEdit by niuben

如何同时绘制多条曲线呢?如何同时绘制多条曲线呢?

命令: subplot命令: subplot

将图像分为不同区域将图像分为不同区域

绘制多条曲线

绘制多条曲线

例子例子

叠加开关命令hold on ( off )叠加开关命令

hold on ( off )同一坐标平面上多曲线叠加同一坐标平面上多曲线叠加

例子例子

Page 20: Matlab  绘图

第第 2020 页,共页,共 122122 页页 Edit by niubenEdit by niuben

7. 例:子图绘制( subplot )7. 例:子图绘制( subplot ) x=-3:0.1:3; y1=x; y2=x.^2; y3=x.^3; y4=x.^4; subplot(2,2,1) % 版面分为 2 行 2 列, % 此为第一图,下同 plot(x,y1) title('y1=x') % 添加标题,下同 subplot(2,2,2) plot(x,y2) title('y2=x^2') subplot(2,2,3) plot(x,y3) title('y3=x^3') subplot(2,2,4) plot(x,y4) title('y4=x^4')

Page 21: Matlab  绘图

第第 2121 页,共页,共 122122 页页 Edit by niubenEdit by niuben

8. 叠加绘图模式 hold8. 叠加绘图模式 hold

叠加绘图开关命令 hold 单独使用可交替 hold on (叠加模式开)与

hold off (叠加模式关)两种状态 Hold all 在实现 hold on 的基础上使得新绘图

循环初始设置的颜色等 在 hold off 状态下,只显示最后绘图指令的结

果,在实际操作中若有多次绘图切记保存已在实际操作中若有多次绘图切记保存已完成结果。完成结果。

Page 22: Matlab  绘图

第第 2222 页,共页,共 122122 页页 Edit by niubenEdit by niuben

例:叠加绘图例:叠加绘图 x=-5:5; y1=randn(size(x)); y2=normpdf(x); subplot(2,1,1) hold hold % 切换子图 1 的叠加绘图模式到关闭状态 plot(x,y1,'b') plot(x,y2,'r') % 新的绘图指令冲掉了原来的绘图结果 title('hold off mode') subplot(2,1,2) hold on % 打开子图 2 的叠加绘图模式 plot(x,y1,'b') plot(x,y2,'r') % 新的绘图结果叠加在原来的图形中 title('hold on mode')

Page 23: Matlab  绘图

第第 2323 页,共页,共 122122 页页 Edit by niubenEdit by niuben

Pause 命令Pause 命令

fplot(@sin,[0,2*pi]);hold;pause;fplot(@cos,[0,2*pi])

Page 24: Matlab  绘图

第第 2424 页,共页,共 122122 页页 Edit by niubenEdit by niuben

9. 设置坐标轴和网格线 * (了解)9. 设置坐标轴和网格线 * (了解) 例: clear clc t=0:0.01*pi:2*pi; x=sin(t); y=cos(t); for i=1:9 subplot(3,3,i) plot(x,y) end subplot(3,3,1) axis auto normal title('axis auto

normal') subplot(3,3,2) axis([-2 2 -1.5 1.5]) axis normal title('axis [] normal') subplot(3,3,3) axis tight normal title('axis tight

normal') subplot(3,3,4) axis auto square title('axis auto

square')

subplot(3,3,5) axis([-2 2 -1.5 1.5]) axis square title('axis [] square') subplot(3,3,6) axis tight square title('axis tight

square') subplot(3,3,7) axis auto equal title('axis auto equal') subplot(3,3,8) axis([-2 2 -1.5 1.5]) axis equal title('axis [] equal') subplot(3,3,9) axis tight equal title('axis tight

equal')

注:对于特殊的坐标轴设置,还可以应用对数 / 半对数模式绘图或双纵轴绘图,需要时,同学们可以学习函数 semilogx/semilogy

/loglog/plotyy 等等

Page 25: Matlab  绘图

第第 2525 页,共页,共 122122 页页 Edit by niubenEdit by niuben

例 * :半对数绘图例 * :半对数绘图 x=0:0.04*pi:20*pi; y=exp(x); subplot(1,2,1); plot(x,y); subplot(1,2,2); semilogy(x,y);

Page 26: Matlab  绘图

第第 2626 页,共页,共 122122 页页 Edit by niubenEdit by niuben

10. 图形标注10. 图形标注

title xlabel ylabel legend colorbar annotation( 绘图 ) text 、 gtext ……注 1. 以上命令原则上均可以在绘图窗口通过可视化操作实现

(例如 insert 菜单下命令),请注意老师演示。注 2. 图形标注时可以使用 TEX 命令,例如用 \alpha表示希腊字母 , \it表示斜体等等。

Page 27: Matlab  绘图

第第 2727 页,共页,共 122122 页页 Edit by niubenEdit by niuben

例 .坐标轴和标签例 .坐标轴和标签 x=[1990:2:2000]; y=[1.25 0.81 2.16 2.73 0.06 0.55]; xin=1990:0.2:2000; yin=spline(x,y,xin); plot(x,y,'ob',xin,yin,'-.r') title('1990年到 2000年某地区年平均降水量图 ') xlabel('\it年份 ','FontSize',15) ylabel('降雨量 ','FontSize',8)

Page 28: Matlab  绘图

第第 2828 页,共页,共 122122 页页 Edit by niubenEdit by niuben

11. 特殊绘图11. 特殊绘图

1. 柱状图例、柱状图%………………………………X=[3 2 1;4 2 7;3 6 9;5 1 7];subplot(1,2,1)bar(X)subplot(1,2,2)barh(X,'stacked')%………………………………

Page 29: Matlab  绘图

第第 2929 页,共页,共 122122 页页 Edit by niubenEdit by niuben

11. 特殊绘图(续)11. 特殊绘图(续)

2. 直方图例、直方图%………………………………x=randn(1000,1);subplot(2,1,1)hist(x)subplot(2,1,2)hist(x,50)%………………………………% 极坐标下直方图用 Rose 函数% 玫瑰图,略

Page 30: Matlab  绘图

第第 3030 页,共页,共 122122 页页 Edit by niubenEdit by niuben

11. 特殊绘图(续)11. 特殊绘图(续)

3. 饼图例、饼图%………………………………x=rand(1,5)y=[0.2 0.45 0.1];subplot(1,2,1)pie(x)subplot(1,2,2)pie(y)% 总和超过一时% 求比例,小于% 一则空缺处理

Page 31: Matlab  绘图

第第 3131 页,共页,共 122122 页页 Edit by niubenEdit by niuben

11. 特殊绘图(续)11. 特殊绘图(续)

4. 离散数据绘图(了解)例、火柴杆图%………………………………t=0:0.2:10;y=exp(-0.2*t).*cos(7*t);subplot(2,1,1)stem(t,y)hold onplot(t,y)plot(t,y,'r')subplot(2,1,2)stem(t,y,'-.dg','fill')

Page 32: Matlab  绘图

第第 3232 页,共页,共 122122 页页 Edit by niubenEdit by niuben

11. 特殊绘图(续)11. 特殊绘图(续)

4. 离散数据绘图(了解)例、阶梯图%………………………………t=0:0.5:10;y=exp(-0.2*t).*cos(t);stairs(t,y)hold onplot(t,y,'r')

Matlab 根据需要还可以绘制向量场图、彗星线、等高线图等等、需要时可以自学命令:CompassCometFeatherContourQuiver 等等

Page 33: Matlab  绘图

第第 3333 页,共页,共 122122 页页 Edit by niubenEdit by niuben

12. 函数绘图、句柄函数绘图12. 函数绘图、句柄函数绘图

Fplot(fun,limits) Ezplot(fun,[xmin,xmax,ymin,ymax]) ezpolar 、 ezcontour 、 ezcontourf *

例:subplot(2,2,1)fplot(@sin,[0 2*pi])title('y=sin(x)')subplot(2,2,2)f='cos(x)+sin(3*x)';ezplot(f,[-2*pi 2*pi -3 3])title('y=cos(x)+sin(3x)')subplot(2,2,3)ezpolar('2+3*sin(t)+5*cos(t)',[0 2*pi])title('\rho=sin(\theta)+cos(\theta)')subplot(2,2,4)ezcontourf(@peaks)

Page 34: Matlab  绘图

第第 3434 页,共页,共 122122 页页 Edit by niubenEdit by niuben

想一想,以下代码运行后是什么结果?想一想,以下代码运行后是什么结果?

x=sym('x') f = 1/(5+4*cos(x)) subplot(3,1,1);ezplot(f)

f1 = diff(f) subplot(3,1,2); ezplot(f1)

f2 = diff(f,2) subplot(3,1,3); ezplot(f2)

Page 35: Matlab  绘图

第第 3535 页,共页,共 122122 页页 Edit by niubenEdit by niuben

13.工作的保存13.工作的保存

Save Save as Generate M-file

Page 36: Matlab  绘图

第第 3636 页,共页,共 122122 页页 Edit by niubenEdit by niuben

(二)、三维图形(二)、三维图形

1. 数据准备 *2. 设置绘图区3. 绘图 *4. 设置视角5. 设置颜色表6. 设置光照效果7. 设置坐标轴8. 标注图形9. 保存和导出图形

Matlab 三维绘图的基本流程Matlab 三维绘图的基本流程

Page 37: Matlab  绘图

第第 3737 页,共页,共 122122 页页 Edit by niubenEdit by niuben

三维图形 效果三维图形 效果 x=-8:.1:8; y=-8:.1:8; [X,Y]=meshgrid(x,y); Z=(exp(X)-exp(Y)).*sin(X-Y);% 数据准备完成 figure;%创建绘图 surf(X,Y,Z);% 绘图 view([75 25]); % 视角 colormap hsv; %Hue – saturation - value shading interp;% 颜色表 light('position',[1 .5 .5]);%光源位置 lighting gouraud;%类型 material metal;%反射特性 axis square; colorbar; print;

Page 38: Matlab  绘图

第第 3838 页,共页,共 122122 页页 Edit by niubenEdit by niuben

1. 三维曲线绘制: plot3 函数1. 三维曲线绘制: plot3 函数 用法与 plot 基本相同 %………………………… close all x=-5:0.4:5; y=5:-0.4:-5; z=exp(-0.2*x).*cos(y); [X,Y]=meshgrid(x,y); Z=exp(-0.2*X).*cos(Y); figure subplot(2,1,1) plot3(x,y,z,'or',x,y,z) subplot(2,1,2) plot3(X,Y,Z) %…………………………

Page 39: Matlab  绘图

第第 3939 页,共页,共 122122 页页 Edit by niubenEdit by niuben

附:与 plot ( plot3 )相似的 comet ( comet3 )函数 例: t=1:.1:400;x=sin(t);y=cos(t); comet3(t,x,y) 同学们可以自己观察结果

Page 40: Matlab  绘图

第第 4040 页,共页,共 122122 页页 Edit by niubenEdit by niuben

2. 三维曲面绘制:网线图和表面图2. 三维曲面绘制:网线图和表面图( 1 )网线图 Mesh close all clear [X,Y] = meshgrid(-3:.5:3); Z = 2*X.^2-3*Y.^2; subplot(2,2,1) plot3(X,Y,Z) title('plot3') subplot(2,2,2) mesh(X,Y,Z) title('mesh') subplot(2,2,3) meshc(X,Y,Z) title('meshc') subplot(2,2,4) meshz(X,Y,Z) title('meshz')

Page 41: Matlab  绘图

第第 4141 页,共页,共 122122 页页 Edit by niubenEdit by niuben

2. 三维曲面绘制:网线图和表面图2. 三维曲面绘制:网线图和表面图( 2 )表面图 Surf close all clear [X,Y] = meshgrid(-3:.5:3); Z = 2*X.^2-3*Y.^2; subplot(2,2,1) mesh(X,Y,Z) title('mesh') subplot(2,2,2) surf(X,Y,Z) title('surf') subplot(2,2,3) surfc(X,Y,Z) title('surfc') subplot(2,2,4) surfl(X,Y,Z) title('surfl')

Page 42: Matlab  绘图

第第 4242 页,共页,共 122122 页页 Edit by niubenEdit by niuben

2. 三维曲面绘制:网线图和表面图2. 三维曲面绘制:网线图和表面图

( 3 )网格边框设置: hidden close all clear [X,Y] = meshgrid(-3:.25:3); Z = -sqrt(X.^2+3*Y.^2); subplot(1,2,1) mesh(X,Y,Z) hidden on title('hidden on') subplot(1,2,2) mesh(X,Y,Z) hidden off title('hidden off')

Page 43: Matlab  绘图

第第 4343 页,共页,共 122122 页页 Edit by niubenEdit by niuben

3. 三维特殊绘图3. 三维特殊绘图( 1 )柱状图 bar3 、 bar3h clear x=rand(3,10); subplot(2,2,1) bar(x) title('bar') subplot(2,2,2) barh(x,'stack') title('barh-stack') subplot(2,2,3) bar3(x) title('bar3') subplot(2,2,4) bar3h(x,'stack') title('bar3h-stack')

Page 44: Matlab  绘图

第第 4444 页,共页,共 122122 页页 Edit by niubenEdit by niuben

3. 三维特殊绘图3. 三维特殊绘图

( 2 )饼图 x=[32 45 11 76 56]; explode=[0 0 1 0 1]; pie3(x,explode)

Page 45: Matlab  绘图

第第 4545 页,共页,共 122122 页页 Edit by niubenEdit by niuben

3. 三维特殊绘图3. 三维特殊绘图

( 3 )等值线图 clear [X,Y]=meshgrid(-3:0.01:3); Z=X.^2+Y.^2; contour3(X,Y,Z,20) view([45 50])

Page 46: Matlab  绘图

第第 4646 页,共页,共 122122 页页 Edit by niubenEdit by niuben

3. 三维特殊绘图3. 三维特殊绘图

( 3 )其他 Ezplot3 Ezmesh Ezsurf Stem3 Quiver3 …………

Page 47: Matlab  绘图

第第 4747 页,共页,共 122122 页页 Edit by niubenEdit by niuben

作业 I作业 I

# 利用作图的方法观察以下方程在区间【 -10 , 10 】上解的个数 14*sin(x).^4+5*exp(x.^2)-3*x.^3-x.^6-10=0Hint : plot(x,y);axis([-10 10 -.5 .5]) # 绘制平面上的一条双曲线,并且画出它的两条渐近线。 # 画出三维空间的单位球面,并且画出 oxy 平面上的赤道线。Hint:利用 sphere 生成球面上的点,再利用 mesh画出球面,最后用 plot画线 统计你和你身边同学上大学以来的各科成绩,并且画出柱状统计图。 *利用 Matlab 的帮助文档完成以下命令的学习: plotyy (双纵轴绘图) comet ( 3 )(彗星图) Ezplot3 Ezmesh Ezsurf

……