Transcript 111

Matlab 绘图
2012.10.29
Edit by niuben
主要内容:
1
2
3
4
5
6
Edit by niuben
第2页,共122页
引出:

观察这组数据(500个),看看有什么规律?
-0.4326
-1.6656
0.1253
0.2877
-1.1465
1.1909
1.1892
-0.0376
0.3273
0.1746
-0.1867
0.7258
-0.5883
2.1832
-0.1364
0.1139
1.0668
0.0593
-0.0956
-0.8323
0.2944
-1.3362
Edit by niuben
第3页,共122页
描点看规律??
Edit by niuben
第4页,共122页
应用MATLAB对500组数据作出统计规律图
可以看出这是一组正态分布的数据。都有哪些手段可以让数据的规律或性
质更直接的展示出来呢?
Edit by niuben
第5页,共122页
一、Matlab绘图问题
One
Two
Edit by niuben
二维图形
散点、折线、一般曲线、极坐
标、柱状统计图、饼图、等高
线、向量图、图形修饰等等。
三维图形
曲线、曲面、柱面、散点、
饼图等等
第6页,共122页
(一)、二维图形
Matlab 二维绘图的基本流程
1.
2.
3.
4.
5.
6.
7.
Edit by niuben
数据准备*(必要步骤)
设置绘图区
绘图*
设置曲线和记点格式
设置坐标轴和网格线属性
标注图形
保存和导出图形
第7页,共122页
二维图形
画线函数:line
核心绘图函数:plot
极坐标绘图:polar
基本绘图函数
Edit by niuben
第8页,共122页
1. line函数绘图*(了解,下同)
%……………………
x=0:0.4*pi:2*pi;
y=sin(x);
line(x,y)
%……………………
%line(x,y)将(x(i),y(i))
%代表的点用线段依次连起
%本书部分例题选自《精通Matlab7》
Edit by niuben
第9页,共122页
2.plot函数绘图语法
plot函数是Matlab中最核心的二维绘图
函数,它有多种语法格式,可以实现
很多功能。
例如:plot(y);plot(x,y)(y可
以是多维数组);plot(x1,y1,x2,
y2……)等等。
Edit by niuben
第10页,共122页
例: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)
%1
plot(x,y1)
%2
plot(x,[y1;y2;y3;y4])
%3
plot(x,y1,x,y2,x,y3,x,y4) %4
%……………………
Edit by niuben
第11页,共122页
help plot (Matlab帮助)
PLOT
Linear plot.
PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix,
then the vector is plotted versus the rows or columns of the matrix,
whichever line up. If X is a scalar and Y is a vector, length(Y)
disconnected points are plotted.
PLOT(Y) plots the columns of Y versus their index.
If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)).
In all other uses of PLOT, the imaginary part is ignored.
Various line types, plot symbols and colors may be obtained with
PLOT(X,Y,S) where S is a character string made from one element
from any or all the following 3 columns:
b
blue
.
point
solid
g
green
o
circle
:
dotted
r
red
x
x-mark
-.
dashdot
c
cyan
+
plus
-dashed
m
magenta
*
star
(none) no line
y
yellow
s
square
k
black
d
diamond
v
triangle (down)
^
triangle (up)
<
triangle (left) >
triangle (right)
p
pentagram
hexagram
For example, PLOT(X,Y,'c+:') plots a cyan dotted line with a plus
at each data point; PLOT(X,Y,'bd') plots blue diamond at each data
point but does not draw any line.
PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by
the (X,Y,S) triples, where the X's and Y's are vectors or matrices
and the S's are strings.
For example, PLOT(X,Y,'y-',X,Y,'go') plots the data twice, with a
Edit by niuben
第12页,共122页
h
例. 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]);
Edit by niuben
第13页,共122页
3.例:极坐标绘图*
思考:此处2*pi 若改为pi 或0.5*pi 或0.25pi,结果如何?
polar函数格式:polar(theta,rho)
例:
%……………………
theta=0:0.05*pi:2*pi;
r1=sin(theta);
r2=cos(theta);
polar([theta' theta'],[r1' r2'])
%……………………
Edit by niuben
第14页,共122页
4.曲线格式和标记点格式
曲线线形 颜色
-.
-:
实线
点划线
虚线
点线
r 红色
g绿色
b蓝色
k黑色
y黄色
c蓝绿色
m洋红色
w白色
标记点格式
+、 o、 *、 .、X、 s(方格) 、d(菱形)、 ^(上三角)、 v(下三角)、
<(左三角 )、>(右三角)、 p(五边形)、 h(六边形)。
Edit by niuben
第15页,共122页
例: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')
%第一组数据只标记数据点
%……………………
:vr(点、下三角红色)
Edit by niuben
第16页,共122页
--dc(虚线菱形蓝绿)
5.曲线格式(线宽、标记点大小,边框、颜色)*
格式:
plot(…,’PropertyName’,PropertyValue)
Edit by niuben
PropertyName
意义
选项
LineWidth
线宽
数值
MarkerEdgeColor
标记点边框颜色
颜色字符,’r’……
MarkerFaceColor
标记点填充颜色
同上
MarkerSize
标记点大小
数值
第17页,共122页
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)
%……………………
Edit by niuben
第18页,共122页
如何同时绘制多条曲线呢?
命令:subplot
将图像分为不同区域
例子
绘制多
条曲线
同一坐标平面
上多曲线叠加
叠加开关命令
hold on(off)
例子
Edit by niuben
第19页,共122页
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')
Edit by niuben
第20页,共122页
8.叠加绘图模式 hold
 叠加绘图开关命令hold
 单独使用可交替hold on(叠加模式开)与
hold off(叠加模式关)两种状态
 Hold all 在实现hold on的基础上使得新绘图
循环初始设置的颜色等
 在hold off状态下,只显示最后绘图指令的结果,
在实际操作中若有多次绘图切记保存已完
成结果。
Edit by niuben
第21页,共122页
例:叠加绘图


















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')
Edit by niuben
第22页,共122页
Pause 命令
 fplot(@sin,[0,2*pi]);hold;pause;fplot(
@cos,[0,2*pi])
Edit by niuben
第23页,共122页
9.设置坐标轴和网格线*(了解)






















Edit by niuben
例: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')
第24页,共122页

注:对于特殊的坐标轴
设置,还可以应用对数/
半对数模式绘图或双纵
轴绘图,需要时,同学
们可以学习函数
semilogx/semilogy
/loglog/plotyy等等
例*:半对数绘图






Edit 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);
第25页,共122页
10.图形标注
 title
 xlabel
 ylabel
 legend
 colorbar
 annotation(绘图)
 text、gtext
 ……
注1.以上命令原则上均可以在绘图窗口通过可视化操作实现
(例如insert菜单下命令),请注意老师演示。
注2. 图形标注时可以使用TEX命令,例如用\alpha表示希腊
字母  ,\it表示斜体等等。
Edit by niuben
第26页,共122页
例.坐标轴和标签








Edit 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)
第27页,共122页
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')
%………………………………
Edit by niuben
第28页,共122页
11.特殊绘图(续)
2.直方图
例、直方图
%………………………………
x=randn(1000,1);
subplot(2,1,1)
hist(x)
subplot(2,1,2)
hist(x,50)
%………………………………
%极坐标下直方图用Rose函数
%玫瑰图,略
Edit by niuben
第29页,共122页
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)
%总和超过一时
%求比例,小于
%一则空缺处理
Edit by niuben
第30页,共122页
11.特殊绘图(续)
4.离散数据绘图(了解)
例、火柴杆图
%………………………………
t=0:0.2:10;
y=exp(-0.2*t).*cos(7*t);
subplot(2,1,1)
stem(t,y)
hold on
plot(t,y)
plot(t,y,'r')
subplot(2,1,2)
stem(t,y,'-.dg','fill')
Edit by niuben
第31页,共122页
11.特殊绘图(续)
4.离散数据绘图(了解)
例、阶梯图
%………………………………
t=0:0.5:10;
y=exp(-0.2*t).*cos(t);
stairs(t,y)
hold on
plot(t,y,'r')
Matlab根据需要还可以绘制
向量场图、彗星线、等高线
图等等、需要时可以自学命
令:
Compass
Comet
Feather
Contour
Quiver等等
Edit by niuben
第32页,共122页
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)
Edit by niuben
第33页,共122页
想一想,以下代码运行后是什么结果?
 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)
Edit by niuben
第34页,共122页
13.工作的保存
 Save
 Save as
 Generate M-file
Edit by niuben
第35页,共122页
(二)、三维图形
Matlab 三维绘图的基本流程
1.
2.
3.
4.
5.
6.
7.
8.
9.
Edit by niuben
数据准备*
设置绘图区
绘图*
设置视角
设置颜色表
设置光照效果
设置坐标轴
标注图形
保存和导出图形
第36页,共122页
三维图形 效果















Edit 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;
第37页,共122页
1.三维曲线绘制:plot3函数














Edit by niuben
用法与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)
%…………………………
第38页,共122页
附:与plot(plot3)相似的comet(comet3)函数




例:
t=1:.1:400;x=sin(t);y=cos(t);
comet3(t,x,y)
同学们可以自己观察结果
Edit by niuben
第39页,共122页
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')
Edit by niuben
第40页,共122页
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')
Edit by niuben
第41页,共122页
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')
Edit by niuben
第42页,共122页
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')
Edit by niuben
第43页,共122页
3.三维特殊绘图
(2)饼图
 x=[32 45 11 76 56];
 explode=[0 0 1 0 1];
 pie3(x,explode)
Edit by niuben
第44页,共122页
3.三维特殊绘图
(3)等值线图





Edit by niuben
clear
[X,Y]=meshgrid(-3:0.01:3);
Z=X.^2+Y.^2;
contour3(X,Y,Z,20)
view([45 50])
第45页,共122页
3.三维特殊绘图
(3)其他






Edit by niuben
Ezplot3
Ezmesh
Ezsurf
Stem3
Quiver3
…………
第46页,共122页
作业I
#利用作图的方法观察以下方程在区间【-10,10】上解的个数
14*sin(x).^4+5*exp(x.^2)-3*x.^3-x.^6-10=0
Hint:plot(x,y);axis([-10 10 -.5 .5])
 #绘制平面上的一条双曲线,并且画出它的两条渐近线。
 #画出三维空间的单位球面,并且画出oxy平面上的赤道线。
Hint:利用sphere 生成球面上的点,再利用mesh画出球面,最后用plot画线
 统计你和你身边同学上大学以来的各科成绩,并且画出柱状统计图。
 *利用Matlab的帮助文档完成以下命令的学习:
plotyy(双纵轴绘图)
comet(3)(彗星图)
 Ezplot3
 Ezmesh
 Ezsurf

……
Edit by niuben
第47页,共122页