1 / 32

第五讲 MATLAB 高级图形绘制技术

第五讲 MATLAB 高级图形绘制技术. 5.1 其他二维图形绘制命令 5.2 其他三维图形绘制命令 5.3 句柄图形绘制技术 5.4 图形可视编辑工具 5.5 视角变换与三视图 5.6 图像文件的读写与显示 5.7 MATLAB 图形输出技术 5.8 例子与习题. 5.1 其他二维图形绘制技术. 首先介绍 ezplot 函数,下表中 f, x, y 为函数或表达式. 5.1 其他二维图形绘制技术. 例子. ezplot('cos(x)', [0, pi]). ezplot('x^3 + 2*x^2 - 3*x + 5 - y^2').

aysha
Télécharger la présentation

第五讲 MATLAB 高级图形绘制技术

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第五讲 MATLAB高级图形绘制技术 5.1 其他二维图形绘制命令 5.2 其他三维图形绘制命令 5.3 句柄图形绘制技术 5.4 图形可视编辑工具 5.5 视角变换与三视图 5.6 图像文件的读写与显示 5.7 MATLAB 图形输出技术 5.8 例子与习题

  2. 5.1 其他二维图形绘制技术 首先介绍 ezplot函数,下表中f, x, y 为函数或表达式

  3. 5.1 其他二维图形绘制技术 例子 ezplot('cos(x)', [0, pi]) ezplot('x^3 + 2*x^2 - 3*x + 5 - y^2') f = inline('cos(x)+2*sin(x)'); ezplot(f) ezplot('x^2 + y^2 - 1',[-1.25,1.25]); axis equal ezplot('sin(3*t)*cos(t)','sin(3*t)*sin(t)',[0,pi]) ezplot('t*cos(t)','t*sin(t)',[0,4*pi])

  4. 5.1 其他二维图形绘制技术 fplot函数, fplot('function',limits) fplot plots a function between specified limits. The function must be of the form y = f(x), where x is a vector whose range specifies the limits, and y is a vector the same size as x and contains the function's value at the points in x (see the first example). If the function returns more than one value for a given x, then y is a matrix whose columns contain each component of f(x) (see the second example). 例一 fplot('tanh',[-2 2]) 例二 Create an M-file, myfun, that returns a two column matrix: function Y = myfun(x) Y(:,1) = 200*sin(x(:))./x(:); Y(:,2) = x(:).^2; Plot the function with the statement: fplot('myfun',[-20 20] 例三 fplot('[tan(x),sin(x),cos(x)]',2*pi*[-1 1 -1 1])

  5. 5.2 其他三维图形绘制技术 MATLAB除了 plot3 外还提供有大量其他的三维图形绘制,可以用 help specgraph命令,与二维对应的有以下一些。

  6. 5.2 其他三维图形绘制技术 [x, y, z]=sphere(n)球面 [x, y, z]=cylinder(R, n)柱体 [x, y, z]=ellipsoid(xc, yc, zc, xr, yr, zr, n)椭圆体 subplot(221),sphere(3),title('N=3') subplot(222),sphere(6),title('N=6') subplot(223),sphere(20),title('N=20') subplot(224),sphere(50),title('N=50')

  7. 5.2 其他三维图形绘制技术 [x, y, z]=cylinder(R, n)柱体R 为柱面各个层次上的半径 R=[5 0] subplot(221),cylinder(R, 3),title('N=3') subplot(222), cylinder(R,6),title('N=6') subplot(223), cylinder(R),title('N=20') subplot(224), cylinder(R,50),title('N=50')

  8. 5.2 其他三维图形绘制技术 [x, y, z]=ellipsoid(xc, yc, zc, xr, yr, zr, n)圆心为xc, yc, zc, 半径为 xr, yr, zr。 subplot(221), ellipsoid(0,0,0,1,2,3, 3),title('N=3') subplot(222), ellipsoid(0,0,0,1,2,3, 6),title('N=6') subplot(223), ellipsoid(0,0,0,1,2,3),title('N=20') subplot(224), ellipsoid(0,0,0,1,2,3, 50),title('N=50')

  9. 5.2 其他三维图形绘制技术 三维表面网格图: mesh(x, y, z, c) x, y 分别构成该曲面的 x 和 y 矩阵 z 为高度矩阵, c 为 颜色矩阵 一般来说, x, y 可以由函数 meshgrid函数来生成。 三维曲面还可以由函数 surf(x, y, z, c)画出,参数含义同上,区别见下例子。 隐含的部分可以由 hidden on或 hidden off来显示或隐含。 还可以用 colorbar函数在三维曲面边上显示一颜色条。

  10. 5.2 其他三维图形绘制技术 例子:画出下列函数的三维表面图形 [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); mesh(x,y,z)

  11. 5.2 其他三维图形绘制技术 >> [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); >> z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); surf(x,y,z) 同时观察 colorbar, hidden on, hidden off的影响

  12. 5.2 其他三维图形绘制技术 三维表面着色 shading三种选择 faceted, flat 和 interp 光照点位置 light( ‘Position’, [x, y, z]), 对应用 surf 的另一个版本 surfl 色调方案 colormap(MAP),其中 MAP 是一个 nX3 的矩阵,三列对应 R, G, B, 预定义的色调方案有 hot, cool, copper, pink, gray, bone, prism, flag 局部图形剪切:将要剪切的部分用数值 NaN 代替。

  13. 5.2 其他三维图形绘制技术 [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); surfl(x,y,z) light('pos',[-3 2 1.5]);shading flat; 例子:

  14. 5.2 其他三维图形绘制技术 [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); surfl(x,y,z) light('pos',[-3 2 1.5]);shading flat; colormap(hot); 例子:

  15. 5.2 其他三维图形绘制技术 等高线 contour(x, y, z, n)或 contour3(x, y, z, n) [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); surfl(x,y,z) subplot(121); contour(x, y, z, 50), subplot(122), contour3(x, y, z, 50);

  16. 5.2 其他三维图形绘制技术 瀑布式曲面 waterfall(x, y, z ) [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); waterfall(x, y, z)

  17. 5.3 句柄图形绘制技术 MATLAB中大部分绘图命令会返回一个句柄,它代表所绘制图形对象,如:坐标轴、线条、文字等,然后可以用 set, get 命令来设置或获得该图形对象的有关属性,代表了面向对象编程的思想。MATLAB中图形对象层次关系如下图所示。

  18. 5.3 句柄图形绘制技术 用下面命令格式来设置或获得有关属性值 set(H, ‘PropertyName’, PropertyValue, …) PropertyValue = get(H, ‘PropertyName’) • 如下述命令 h=figure; get(h); • MATLAB将列出当前图形窗口一系列的属性名称 • 如下述命令 h=axes; get(h); • MATLAB将列出当前坐标轴一系列的属性名称 • 如下述命令 t=0:0.1:2*pi; y=sin(t); h=plot(t,y); get(h) • MATLAB将列出当前线条一系列的属性名称 • 命令 set(h) 列出图形对象 h 的所有属性及取值

  19. 5.3 句柄图形绘制技术 常用的通用对象属性

  20. 5.3 句柄图形绘制技术 常用的坐标轴对象属性 同时,Xlabel, X…. 等属性还有对应Y轴、Z轴的同样的属性

  21. 5.3 句柄图形绘制技术 常用的文字对象属性

  22. 5.3 句柄图形绘制技术 常用的线条对象属性

  23. 5.3 句柄图形绘制技术 >> clf; t=0:0.4:2*pi; hc=plot(t, sin(t), '-pentagram'); >> ht=gtext(' This is the original text'); >> y1=get(hc, 'YData'); >> set(hc, 'MarkerSize', 20, 'LineStyle','-.','Color','k','YDaa', 2*y1); >> set(hc, 'MarkerSize', 20, 'LineStyle','-.','Color','k','YData', 2*y1); >> set(ht, 'String', 'Udated Text', 'FontSize', 20, 'Rotation', 10);

  24. 5.4 图形可视编辑技术 前面说的图形对象的属性设置或修改也可以用图形界面 GUI 的形式来方便地进行。可以从图形窗口的 EDIT 菜单下或用命令 protedit来激活图形可视编辑窗口。

  25. 5.4 图形可视编辑技术

  26. 5.5 视角变换与三视图 三维图形绘制中的视角定义

  27. 5.5 视角变换与三视图 MATLAB提供了一个函数 view 来改变 和 的值, 设置视角数据 view(az, el) , view([az, el]), view(T) 获得视角数据[az, el]= view, [az, el]=view(3), T=view [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); subplot(121),surf(x,y,z);[a,b]=view; subplot(122),surf(x,y,z);view(-a,b);

  28. 根据视角的不同,MATLAB 可以画出类似于机械制图中常用的三视图。 5.5 视角变换与三视图 [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); subplot(221),surf(x,y,z),shading flat, axis([-3 3 -2 2 -0.7 1.5]);view(0,90),title('view(0,90)') subplot(222),mesh(x,y,z),axis([-3 3 -2 2 -0.7 1.5]);view(90,0),title('view(90,0)') subplot(223),mesh(x,y,z),axis([-3 3 -2 2 -0.7 1.5]);view(0,0),title('view(0,0)') subplot(224),mesh(x,y,z),axis([-3 3 -2 2 -0.7 1.5]);title('normal viewpoint')

  29. 用 surf 将得到更光滑的图形。 5.5 视角变换与三视图 [x,y]=meshgrid(-3:0.1:3,-2:0.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); subplot(221),surf(x,y,z),shading interp, axis([-3 3 -2 2 -0.7 1.5]);view(0,90),title('view(0,90)') subplot(222),surf(x,y,z),shading interp,axis([-3 3 -2 2 -0.7 1.5]);view(90,0),title('view(90,0)') subplot(223),surf(x,y,z), shading interp,axis([-3 3 -2 2 -0.7 1.5]);view(0,0),title('view(0,0)') subplot(224),surf(x,y,z), shading interp,axis([-3 3 -2 2 -0.7 1.5]);title('normal viewpoint')

  30. 5.6 图像文件的读写与显示 MATLAB 提供了一组图像读写和显示功能的函数 除了上述几个基本函数外,MATLAB 还提供了一个图像处理工具箱,包含了大量的图像处理函数 支持的图像格式有:bmp, jpg, hdf, pcx, png, tiff, xwd 等

  31. 5.6 图像文件的读写与显示 MATLAB 提供了一组图像读写和显示功能的函数 >> img=imread('greens.jpg'); >> whos Name Size Bytes Class img 300x500x3 450000 uint8 array Grand total is 450000 elements using 450000 bytes >> imwrite(img, 'test.bmp','bmp'); >> image(img)

  32. 5.7 MATLAB图形输出技术 选择图形窗口的 Edit 下的 Copy 命令可以把图形图像赋制导系统的剪切板中,也可以用 print -device -options filename命令格式输出到指定的设备文件上。-device 选项意义如下表。 • 打印机直接输出 -dwin单色打印机,-dwinc彩色打印机 • 页面描述语言格式 -dps单色PostScript格式,-dpsc彩色PostScript格式; -deps单色 EPS 文件,-depsc彩色 EPS文件。 • 其他常用图形文件格式 -dhpg1(HP绘图仪)、-djpeg(JPEG文件格式)、-dtiff(TIFF文件格式)。 • 复制到剪切板 -dmeta以图元文件格式, -dbitmap位图格式。 >> print -deps aaa

More Related