📄 18.txt
字号:
MMCONT2(X,Y,Z,C) plots N contours of Z in 2-D using the color specified in C.C can be
a linestyle and color as used in plot,e.g., ‘ r- ‘ ,or C can be the string name of a colormap.X and Y.
define the axis limits.
If not given default argument values are :N=10,C= ‘ hot ‘ ,X and Y =row and column indices of Z.Examples:
MMCONT2(Z) 10 lines with hot colormap
MMCONT2(Z,20) 20 lines with hot colormap
MMCONT2(Z, ‘ copper ‘ ) 10 lines with copper colormap
MMCONT2(Z,20, ‘ gray ‘ ) 20 lines with gray colormap
MMCONT2(X,Y,Z, ‘ jet ‘ ) 10 lines with jet colormap
MMCONT2(Z, ‘ c-- ‘ ) 10 dashed lines in cyan
MMCONT2(X,Y,Z,25, ‘ pink ‘ ) 25 lines in pink colormap
CS=MMCONT2( . . .) returns the contour matrix CS as described in CONTOURC.
[CS,H]=MMCONT2( . . .) returns a column vector H of handles to line objects
帮助信息:
MMCONT2(X,Y,Z,N,C)用由C指定的颜色在二维平面内画出关于Z方向的N条等值线。C 可以是在函数plot中使用的线形和颜色,例如 ‘ r- ‘ ;或者是一个字符串指明颜色映象名。 X和Y指定了坐标轴的范围。
如果未指定参数,缺省的参数值是:N=10,C= ‘ hot ‘ ,X和Y分别是Z的行列下标。 例子如下:
MMCONT2(Z) 使用暖色颜色映象画10条等值线
MMCONT2(Z,20) 使用暖色颜色映象画20条等值线
MMCONT2(Z, ‘ copper ‘ ) 使用铜黄色颜色映象画10条等值线
MMCONT2(Z,20, ‘ gray ‘ ) 使用灰色颜色映象画20条等值线
MMCONT2(X,Y,Z, ‘ jet ‘ ) 使用‘jet’暖色颜色映象画10条等值线
MMCONT2(Z, ‘ c-- ‘ ) 使用青蓝色颜色映象用虚划线画10条等值线
MMCONT2(X,Y,Z,25, ‘ pink ‘ ) 使用粉红色颜色映象画25条等值线
CS=MMCONT2(…)返回等值线矩阵CS,它在CONTOURC中描述。
[CS,H]=MMCONT2(…)返回一个包含有线条对象的句柄的列向量。
上面讨论的三维或二维等值线图都假定数据定义在矩形网格或论域内。当不是这种情形时,数据必须被转换到矩形网格上,使等值线图看起来逼真。如本章前面18.4节所述,MATLAB函数griddata能实现所需的转换,更详细的信息参阅该节。
函数surf的二维等效函数是pcolor,它代表伪彩色。
? [X,Y,Z]=peaks(30);
? pcolor(X,Y,Z); % surf plot view from above
? xlabel( ‘ X-axis ‘ ),ylabel( ‘ Y-axis ‘ )
? title( ‘ PCOLOR of PEAKS ‘ )
输出见图18.21.
图18.21 函数PEAKS的伪彩色图
由于这是一个surf图,可以使用函数shading。另外,有时在pcolor图的上面放一个单色等值线图是很有用的。
? [X,Y,Z]=peaks(30);
? pcolor(X,Y,Z);
? shading interp
? hold on
? contour(X,Y,Z,19, ‘ k ‘ ) % add 19 contour lines in black
? xlabel( ‘ X-axis ‘ ),ylabel( ‘ Y-axis ‘ )
? title( ‘ PCOLOR and CONTOUR of PEAKS ‘ )
? hold off
输出见图18.22.
图18.21 函数PEAKS的伪彩色图和等值线图
18.9 其它函数
除了上面讨论的三维函数,MATLAB还提供了函数waterfall,quiver,fill3,和clabel。
函数waterfall与函数mesh一样,只是它的网格线是在x轴方向出现。例如:
? [X,Y,Z]=peaks(30);
? waterfall(X,Y,Z)
? xlabel( ‘ X-axis ‘ ),ylabel( ‘ Y-axis ‘ ),zlabel( ‘ Z-axis ‘ )
输出见图18.23.
图18.23 函数PEAKS的waterfall图
函数quiver在等值线图上画出方向或速度箭头。例如:
? [X,Y,Z]=peaks(16);
? [DX,DY]=gradient(Z, .5, .5);
? contour(X,Y,Z,10)
? hold on
? quiver(X,Y,DX,DY)
? hld off
输出见图18.24.
图18.24 函数PEAKS的quiver图
函数fill3等效于三维函数fill,可在三维空间内画出填充过的多边形。函数fill3(x,y,z,c)使用数组x,y和z作为多边形的顶点而c指定了填充的颜色。例如,下例用随机的顶点坐标值画出五个黄色三角形。
? fill3(rand(3,5),rand(3,5),rand(3,5), ‘ y ‘ )
输出见图18.25.
图18.25 函数fill3图示
函数clabel给等值线图标上高度值。这样做时,函数clabel需要函数contour等值线矩阵的输出。
? [X,Y,Z]=peaks(30);
? cs=contour(X,Y,Z,8); % request output from contour
? clabel(cs) % add labels identifying heights
? xlabel( ‘ X-axis ‘ ),ylabel( ‘ Y-axis ‘ )
? title( ‘ CONTOUR of PEAKS with LABELS ‘ )
输出见图18.26.
图18.26 函数PEAKS的带标志等值线图
18.10 动画
MATLAB提供了一种能力,它可以存储一系列各种类型的二维或三维图,然后象放电影一样把它们按次序重放出来。在某种意义上,动画提供的运动为图形增加另一个维数。通常图形的次序不必以任意的方式关联起来。一种明显的动画类型是取出三维图形然后缓慢地将它旋转,这样我们就可以从不同角度来观察它。另一种类型是当一个参数变化时,依次显示某些问题解的图形。
MATLAB中的函数moviein,getframe和movie提供了捕捉和播放动画的所需工具。函数moviein可以产生一个帧矩阵来存放动画中的帧;函数getframe对当前的图象进行快照;而函数movie按顺序回放各帧。照这样,捕捉和回放动画的方法是:(1)创建帧矩阵;(2)对动画中的每一帧生成图形,并把它捕捉到到帧矩阵里;(3)从帧矩阵里回放动画。
考虑下面的一段脚本M文件例子,例中绘制了函数peaks并且将它绕z轴旋转。
% movie making example: rotate a 3-D surface plot
[X,Y,Z]=peaks(30); % create data
surfl(X,Y,Z) % plot surface with lighting
axis([-3 3 -3 3 -10 10]) % fix axes so that scaling does not change
axis off % erase axes because they jump around
shading interp % make it pretty with interpolated shading
colormap(hot) % choose a good colormap for lighting
m=moviein(15); % choose 15 movie frames for frame matrix m
for I=1:15 % rotate and capture each frame
view(-37.5+24*(i-1),30) % change viewpoint for this frame
m(:,i)=getframe; % add figure to frame matrix
end
movie(m) % play the movie!
注意到动画中的每一帧在帧矩阵中占据一个不同的列。帧矩阵的大小随着动画中的帧数和图形窗口的大小而增加,而与所绘图形的复杂性无关,这是因为函数getframe仅仅是捕捉位图。按缺省,函数movie只放一遍动画。通过加入其它输入参量,它可以向前放、向后倒放、放指定次数或按特定的桢速率播放。关于这些特征的详细信息,参阅MATLAB参考指南或使用在线帮助。
由于上述的动画制作策略很有用,它已体现在精通MATLAB工具箱的函数mmspin3d中。
function M=mmspin3d(n)
% MMSPIN3D Make Movie by 3D Azimuth Rotation of Current Figure.
% MMSPIN3D(N) captures and plays N frames of the current figure
% through one rotation about the Z-axis at the current elevation.
% M=MMSPIN3D(N) returns the movie in M for later playing with movie.
% If not given,N=18 is used.
% MMSPIN3D fixes the axis limits and issues axis off.
% Copyright (c) 1996 by Prentice-Hall,Inc.
if nargin<1,n=18;end
n=max(abs(round(n)),2);
axis(axis);
axis off
incaz=round(360/n);
[az,el]=view;
m=movie(n);
for i=1:n
view(az+incaz*(i-1),el)
m(:,i)=getframe;
end
if nargout, M=m;
else, movie(m);
end
使用mmspin3d,上述脚本简化如下:
% movie-making example:rotate a 3-D surface plot
[X,Y,Z]=peaks(30); % create data
surfl(X,Y,Z) % plot surface with lighting
shading interp % make it pretty with interpolated shading
colormap(hot) % choose a good colormap for lighting
mmspin3d(15)
18.11 小结
本章所讨论的函数和它们的特征总结在表18.2、表18.3、表18.4和表18.5中:
表18.2
三维绘图函数
contour 二维等值线图,即从上向下看contour3等值线图
contour3 等值线图
fill3 填充的多边形
mesh 网格图
meshc 具有基本等值线图的网格图
meshz 有零平面的网格图
pcolor 二维伪彩色绘图,即从上向下看surf图
plot3 直线图
quiver 二维带方向箭头的速度图
surf 曲面图
surfc 具有基本等值线图的曲面图
surfl 带亮度的曲面图
waterfall 无交叉线的网格图
表示18-3
三维绘图工具
axis 修正坐标轴属性
clf 清除图形窗口
clabel 放置等值线标签
close 关闭图形窗口
figure 创建或选择图形窗口
getframe 捕捉动画桢
grid 放置网格
griddata 对画图用的数据进行内插
hidden 隐蔽网格图线条
hold 保留当前图形
meshgrid 产生三维绘图数据
movie 放动画
moviein 创建桢矩阵,存储动画
shading 在曲面图和伪彩色图中用分块、平滑和插值加阴影
subplot 在图形窗口内画子图
text 在指定的位置放文本
title 放置标题
view 改变图形的视角
xlabel 放置x轴标记
ylabel 放置y轴标记
zlabel 放置z轴标记
表18.4
函数view
view(az,el) 设置视图的方位角az和仰角el
view([az,el])
view([x,y,z]) 在笛卡儿坐标系中沿向量[x,y,z]正视原点设置视图,例如view([0 0 1])=view(0,90)
view(2) 设置缺省的二维视图,az=0,el=90
view(3) 设置缺省的三维视图,az=-37.5,el=30
[az,el]=view 返回当前的方位角az和仰角el
view(T) 用一个4×4的转置矩阵T来设置视图
T=view 返回当前的4×4转置矩阵
表18.5
掌握MATLAB高级图形功能
mmcont2(X,Y,Z,C) 具有颜色映象的二维等值线图
mmcont3(X,Y,Z,C) 具有颜色映象的三维等值线图
mmspin3d(N) 旋转当前图形的三维方位角来制作动画
mmview3d 用滑标来调整视角
关键词索引
chap 18
pcolor 伪彩色
3-D helix 三维螺旋线
3-D grid 三维网格
subplot 子图
Figure 图形窗口
viewpoint 视角
elevation 仰角
azimuth 方位角
Handle Graphics 句柄图形
slider 滑标
plaid 方格
mesh plots 网格图
mesh surface 网状曲面
surface 曲面图
contour plot 等值线图
zero plane 零平面
patch 补片
flat shading 平滑加色彩
interpolated shading 插值加色彩
progression 级差
color map 颜色映象
directional or velocity arrows 方向或速度箭头
movie 动画
frame 帧
underlying contour plot 基本等值线图
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -