MATLAB所有画图函数

更新时间:2023-10-20 09:07:01 阅读量: 综合文库 文档下载

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

MATLAB不但擅长於矩阵相关的数值运算,也适合用在各种科学目视表示(Scientific visualization)。下面将介绍MATLAB基本xy平面及xyz空间的各项绘图命令,包含一维曲线及二维曲面的绘制、列印及存档。

plot是绘制一维曲线的基本函数,但在使用此函数之前,我们需先定义曲

线上每一点的x及y座标。下例可画出一条正弦曲线: close all; x=linspace(0, 2*pi, 100); % 100个点的x座标 y=sin(x); % 对应的y座标 plot(x,y);

====================================================

小整理:MATLAB基本绘图函数

plot: x轴和y轴均为线性刻度(Linear scale) loglog: x轴和y轴均为对数刻度(Logarithmic scale) semilogx: x轴为对数刻度,y轴为线性刻度 semilogy: x轴为线性刻度,y轴为对数刻度

====================================================

若要画出多条曲线,只需将座标对依次放入plot函数即可: plot(x, sin(x), x, cos(x));

若要改变颜色,在座标对后面加上相关字串即可:

plot(x, sin(x), 'c', x, cos(x), 'g');

若要同时改变颜色及图线型态(Line style),也是在座标对后面加上相

关字串即可:

plot(x, sin(x), 'co', x, cos(x), 'g*');

====================================================

小整理:plot绘图函数的叁数 字元 颜色 字元 图线型态 y 黄色 . 点 k 黑色 o 圆 w 白色 x x b 蓝色 + + g 绿色 * * r 红色 - 实线 c 亮青色 : 点线 m 锰紫色 -. 点虚线 -- 虚线

====================================================

图形完成后,我们可用axis([xmin,xmax,ymin,ymax])函数来调整图轴的范

围:

axis([0, 6, -1.2, 1.2]);

此外,MATLAB也可对图形加上各种注解与处理: xlabel('Input Value'); % x轴注解 ylabel('Function Value'); % y轴注解

title('Two Trigonometric Functions'); % 图形标题 legend('y = sin(x)','y = cos(x)'); % 图形注解 grid on; % 显示格线

====================================================

我们可用subplot来同时画出数个小图形於同一个视窗之中: subplot(2,2,1); plot(x, sin(x)); subplot(2,2,2); plot(x, cos(x)); subplot(2,2,3); plot(x, sinh(x)); subplot(2,2,4); plot(x, cosh(x));

MATLAB还有其他各种二维绘图函数,以适合不同的应用,详见下表。

====================================================

小整理:其他各种二维绘图函数 bar 长条图

errorbar 图形加上误差范围

fplot 较精确的函数图形 polar 极座标图 hist 累计图 rose 极座标累计图 stairs 阶梯图 stem 针状图 fill 实心图 feather 羽毛图 compass 罗盘图 quiver 向量场图

====================================================

以下我们针对每个函数举例。

当资料点数量不多时,长条图是很适合的表示方式: close all; % 关闭所有的图形视窗 x=1:10; y=rand(size(x)); bar(x,y);

====================================================

如果已知资料的误差量,就可用errorbar来表示: 下例以单位标准差来做资料的误差量:

x = linspace(0,2*pi,30); y = sin(x);

e = std(y)*ones(size(x)); errorbar(x,y,e)

====================================================

对于变化剧烈的函数,可用fplot来进行较精确的绘图, 会对剧烈变化处进行较密集的取样,如下例: fplot('sin(1/x)', [0.02 0.2]); % [0.02 0.2]是绘图范围

====================================================

若要产生极座标图形,可用polar: theta=linspace(0, 2*pi); r=cos(4*theta); polar(theta, r);

对于大量的资料,我们可用h

====================================================

ist来显示资料的分布情况和统计特性。

下面几个命令可用来验证randn产生的高斯乱数分 : x=randn(5000, 1); % 产生5000个 ?=0,?=1 的高斯乱数 hist(x,20); % 20代表长条的个数

t=linspace(0,pi*3,30); x=sin(t); hold on y=cos(t); plot(t,x,'r-',t,y,'g-') grid % 加入格栅 xlabel('x轴') ylabel('y轴')

title('正弦与余弦曲线')

text(1,0,'正弦') %text(x,y,'正弦') text(3,0,'余弦')

legend('sin(x)','cos(x)',3)

%LEGEND('string',Pos) places the legend in the specified, % 0 = Automatic \% 1 = Upper right-hand corner (default) % 2 = Upper left-hand corner % 3 = Lower left-hand corner % 4 = Lower right-hand corner % -1 = To the right of the plot

%按鼠表 left mouse button 拖legend到指定的位置 (2) 子图 clf; hold off

t=linspace(0,pi*3,30); x=sin(exp(t));

subplot (2,2,2) %(n,m,p(0

例:误差图(errorbar) clf;x=0:0.1:4;

y=zeros(size(x));e=rand(size(x)); yu=y+e;yd=y-e; errorbar(x,y,e) hold on

plot(x,yu,'r-');plot(x,yd,'r-');

(3) 绘图工具

mmaxes prop value… 的属性

mmcxy(or)xy—mmcxy 修改绘图坐标轴显示图上鼠标的x-y

坐标

mmdraw prop value… 在图上画直线 rnmfill(x,y,z,c,lb,ub) 填充两条曲线间区域 mmgetxy(N) 使用鼠标获取x-y坐标

mmline prop value… 属性

mmtile 铺多图形窗口

mmtext(' optional text') 本

mrnzoom 皮框缩放坐标轴

mmzap object 标删除文本,线型或坐标轴

mmfont prop value 体属性 例: clf;x=0:0.1:4;

y=zeros(size(x));e=rand(size(x)); yu=y+e;yd=y-e; errorbar(x,y,e)

修改所画线条的 平在图上放置或拖曳文 用橡 使用鼠 修改文本字 hold on

plot(x,yu,'r-');plot(x,yd,'r-'); yu(1)=0;yu(41)=0; fill(x,yu,'r'); yd(1)=0;yd(41)=0; fill(x,yd,'g');

3. 三维图形

(1) plot3(三维直线函数)

以下例子用来体会plot 3的基本的绘图原理。 例:绘参数方程 x=t;y=sin(t);z=cos(t) 的空间曲线 clf

t=0:0.05:100; x=t;y=sin(t);z=sin(2*t); plot3(x,y,z,'b:')

例:空间划线: clf

t=0:0.1:10;x=t;

y=0*ones(size(x));z=sin(t); plot3(x,y,z,'r') hold on

z=0*ones(size(x)); y=sin(t); plot3(x,y,z,'g')

xlabel('x');ylabel('y');zlabel('z');

(2) 三维曲面网格图 例1:划马鞍面: clf x=-4:0.5:4; y=-4:0.5:4;

[U,V]=meshgrid(x,y);

Z=-U.^4+V.^4-U.^2-V.^2-2*U*V; mesh(Z); xlabel('x'); ylabel('y'); zlabel('z');

注1:meshgrid的含义,绘图的基础是网格,一个二元系矩阵[(x j)] hold off a=ones(9); a1=2*ones(5);

,y i

a2=3*ones(2); a(3:7,3:7)=a1; a(5:6,5:6)=a2; meshc(a)

例二:peakS 函数的图形: peakS 函数的表达式

z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) - 1/3*exp(-(x+1).^2 - y.^2) clf;[x,y,z]=peaks(20);p=peaks(20); subplot(2,2,1);mesh(x,y,z) subplot(2,2,2);meshz(y,x,z) subplot(2,2,3);meshc(p) subplot(2,2,4);waterfall(p)

注1:[x,y,z]=peaks(20):为变换角度带来方便。见二图。 p=peaks(20):默认x,y,z的顺序给p赋值。 注2:mesh;meshz;meshc;waterfall,表现上有区别。

注3:peaks 是演示函数。MATLAB中有许多不同的演示函数,与演

示程序(**demo.m)结合在一起。如 peaks 图形演示函数

banane 优化演示函数 (3) 色彩与效果

*1)mesh;SURF;SURFC, SURFL比较 :变更色调(由暖到冷,默认红到兰)的变化方向 shading :涂色方式 clf;x= -1.5:0.2:1.5;y=-1:0.2:1; [X,Y]=meshgrid(x,y); p=sqrt(4-X.^2/9-Y.^2/4);

subplot(3,2,1);mesh(p) 调方向:有上到下

subplot(3,2,2);surf(p) 向:有上到下

subplot(3,2,3);surfc(p) 有上到下

subplot(3,2,4);surfl(p) shading interp 线

subplot(3,2,5);surfl(p)

shading faceted 图,有格线;

;%有网格 格子图,色 ;%默认的方向:色调方 ;%带登高线;色调方向:;%色调方向:沿y轴方向 ; %平滑涂色 图,无格 ;%有网格涂色 格子 subplot(3,2,6);surfl(p)

shading flat ;%有网格涂色 格子图,无格线;

*2) SURFL的z-参数。看z-参数的确定平滑涂色效果(定义变化方向)surfl(p,z);z=(n1,n2,n3)。 clf;

x= -0.5:0.3:2.5;y=-0.5:0.3:2; [X,Y]=meshgrid(x,y); p=(4+X.^2/9+Y.^2/4);; %cm=[1 0 0;0 1 0;0 0 1]; %colormap(cm)

subplot(2,2,1);surfl(p,[1,0,0]) subplot(2,2,2);surfl(p,[0,1,0]) subplot(2,2,3);surfl(p,[0,0,1]) subplot(2,2,4);surfl(p,[1,1,0]) (4) 辅助图视效果 *1)视角定义view(az,el) clf;x= -1.5:0.2:1.5;y=-1:0.2:1; [X,Y]=meshgrid(x,y); p=sqrt(4-X.^2/9-Y.^2/4);

subplot(2,2,1);surfl(p);view(30,30)

shading interp

subplot(2,2,2);surfl(p);view(90,10) shading interp

subplot(2,2,3);surfl(p);view(-10,-10) shading interp

subplot(2,2,4);surfl(p);view(140,60) shading interp

*2)surfl光照模式与光照角度设置, surfl(x,y,z,d,s,k)指令中s 与k 参数 d:见(3)

s:确定光照角度;z=(sx,sy,sz);默认光照角度是观察角逆时针方向45度

k::光照模式:确定强度

ka:背景光 kd:漫射光 ks:定向光 spread:扩散光 例:

clf;x= -1.5:0.2:1.5;y=-1:0.2:1; [X,Y]=meshgrid(x,y); Z=sqrt(4-X.^2/9-Y.^2/4); view(45,45)

subplot(2,2,1);surfl(X,Y,Z, [0,45],[.1 .6 .4 10]); shading interp

subplot(2,2,2);surfl(X,Y,Z, [20,45],[.3 .6 .4 10]); shading interp

subplot(2,2,3);surfl(X,Y,Z, [40,45],[.6 .6 .4 10]); shading interp

subplot(2,2,4);surfl(X,Y,Z, [60,45],[.9 .6 .4 10]); shading interp

*3) 图视放大 zoom on ;zoom off; zoom *鼠标点击变焦(左键放大;右键盘缩小) *鼠标拖拉变焦 t=-16:0.1:16; x=sin(t.*10).*(t.^2); plot(t,x,'r-') zoom on 4. 超维图形表达

(1) 三维色彩表达(色轴;图象的色彩维) clf

a=ones(20); a1=2*ones(13); a2=3*ones(7); a3=4*ones(2); a(4:16,4:16)=a1;

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

Top