~~ *** ~~ *** Matlab plot

Matlab drawing
powerful drawing capabilities is one of the characteristics of Matlab, Matlab provides a set of drawing functions, users do not need too much to consider details of the plot, just give some basic parameters can get the desired pattern, such functions called high-level drawing functions. In addition, Matlab also provides a low-level graphics drawing operations operating handle directly. Such operation pattern of each graphical element (such as coordinate axes, curve, text, etc.) treated as a separate object, the system assigns a handle to each object, the handle can be operated by the graphic element, without affecting the other section.
This chapter describes the rendering of 2D and 3D graphics, and other high-level graphics drawing function usage control function, on this basis, then the low-level drawing operations can be described operation and control of graphic objects.

One. Two-dimensional graphics

Two-dimensional pattern data is a coordinate point on the plane connecting the graphics plane. Different coordinate systems may be employed, such as rectangular coordinates, logarithmic coordinates, polar coordinates, etc. Two-dimensional graphics rendering is the basis for other drawing operations.

One. The basic two-dimensional curve plotted as a function

In Matlab, and the most widely used basic drawing functions as Plot, which use different curves can be drawn on a two dimensional plane.

1. The basic function of usage plot

linear plot function is used to draw a graph on a two-dimensional coordinate plane, to provide a set of y-coordinate and x-coordinate corresponding, respectively, can draw x and y are the horizontal and vertical coordinates of the two-dimensional curve. plot function of the application format
plot(x,y)where x, y is a vector of the same length, storing x and y coordinates.

Example 51 [0, 2pi] interval plotted

Programmed as follows: Enter the following command in the command window

x=0:pi/100:2*pi;
y=2*exp(-0.5*x).*sin(2*pi*x);
plot(x,y)

Here Insert Picture Description
After the execution, a graphics window is opened, in which curves plotted below
NOTE: use a point multiplication between exponential function and a sine function, because the two are vector.

Example 52 plotted

This curve equation is given as parameter, as long as the given parameter vector, and then were obtained x, y to the vector output curve:

t=-pi:pi/100:pi;
x=t.*cos(3*t);
y=t.*sin(t).*sin(t);
plot(x,y)

Here Insert Picture Description
The program is executed, opens a graphics window, where the curve plotted as
the above-mentioned plot function arguments x, y is a vector of the same length, which is the most common, the most basic usage. There are some changes in the practical application. Illustrate:

2. Containing a plurality of input parameters of the plot function

plot function may comprise several sets of vectors, each group may draw a curve. Plot function call having a plurality of input parameters of the format: plot(x1,y1,x2,y2,…,xn,yn)
can draw three curves in the same coordinates as the following commands.

x=linspace(0,2*pi,100);
plot(x,sin(x),x,2*sin(x),x,3*sin(x))

Here Insert Picture Description
When the input parameters in a matrix form, pairs x, y, a corresponding column elements according to the abscissa and ordinate plotted, the curve is equal to the number of the columns of the matrix.

x=linspace(0,2*pi,100);
y1=sin(x);
y2=2*sin(x);
y3=3*sin(x);
x=[x;x;x]';
y=[y1;y2;y3]';
plot(x,y,x,cos(x))

Here Insert Picture Description
x, y matrix containing all three of which make up the input parameters, the three curves plotted; X and cos (x) and are paired, draw a cosine curve.
Plot function using the matrix data may be directly plotted in graphic form, the data in each column of the matrix at this time plot as a function plotted in a graph form. Such as

A=pascal(5)
A =[1 1 1 1 1;1 2 3 4 5;1 3 6 10 15;1 4 10 20 35;1 5 15 35 70];
plot(A)

Here Insert Picture Description

3. With plot function options

Matlab provide some drawing options for determining linear curve depicted, the color marker symbol and the data point. These options are shown in the table:
Here Insert Picture Description

Example with different line and plotted in the same color coordinate and the envelope.

x=(0:pi/100:2*pi)';
y1=2*exp(-0.5*x)*[1,-1];
y2=2*exp(-0.5*x).*sin(2*pi*x);
x1=(0:12)/2;
y3=2*exp(-0.5*x1).*sin(2*pi*x1);
plot(x,y1,'k:',x,y2,'b--',x1,y3,'rp');

Here Insert Picture Description
Plot function included in the three sets of drawing parameters, a first set of two black dashed envelope, the second set of curves are drawn in chain lines y double blue, a third set of discrete five-pointed star shown in red data point.

4. Double ordinate function plotyy

In Matlab, if desired drawing pattern having two different ordinate scaling can be used plotyy function, which can have different dimensions, different number of stages of the same two functions plotted on a coordinate in favor of graphics data Comparative analysis. Using the format: plotyy (X1, Y1, x2, y2)
X1, Y1 corresponds to a curve, x2, y2 corresponds to another curve. The abscissa of the same scale, there are two ordinate, corresponding to the left x1, y1 data, corresponding to the right x2, y2.
Example :( slightly)

two. Graphing of auxiliary operations

After drawn graphics, the graphics may also need some auxiliary operations, in order to make the meaning clearer graphics, more readable.

1. Drawing Label

在绘制图形时,可以对图形加上一些说明,如图形的名称、坐标轴说明以及图形某一部分的含义等,这些操作称为添加图形标注。有关图形标注函数的调用格式为:

title(’图形名称’) (都放在单引号内)
xlabel(’x轴说明’)
ylabel(’y轴说明’)
text(x,y,’图形说明‘)
legend(’图例1’,’图例2’,…) 

Here Insert Picture Description
其中,title、xlabel和ylabel函数分别用于说明图形和坐标轴的名称。text函数是在坐标点(x,y)处添加图形说明。(P88 或用gtext命令)。legend函数用于绘制曲线所用线型、颜色或数据点标记图例,图例放置在空白处,用户还可以通过鼠标移动图例,将其放到所希望的位置。除legend函数外,其他函数同样适用于三维图形,在三维中z坐标轴说明用zlabel函数。
上述函数中的说明文字,除了使用标准的ASCII字符外,还可以使用LaTex(一种流行的数学排版软件)格式的控制字符,这样就可以在图形上添加希腊字符,数学符号和公式等内容。在Matlab支持的LaTex字符串中,用/bf , /it , /rm控制字符分别定义黑体、斜体和正体字符,受LaTex字符串控制部分要加大括号{}括起来。例如,text(0.3,0.5,’the usful {/bf MATLAB}’),将使MATLAB一词黑体显示。一些常用的LaTex字符见表,各个字符可以单独使用也可以和其他字符及命令配合使用。如text(0.3 ,0.5 ,’sin({/omega}t+{/beta})’)
将得到标注效果 :
Here Insert Picture Description

2. 坐标控制

在绘制图形时,Matlab可以自动根据要绘制曲线数据的范围选择合适的坐标刻度,使得曲线能够尽可能清晰的显示出来。所以,一般情况下用户不必选择坐标轴的刻度范围。但是,如果用户对坐标不满意,可以利用axis函数对其重新设定。其调用格式为

axis([xmin xmax ymin ymax zmin zmax])

如果只给出前四个参数,则按照给出的x、y轴的最小值和最大值选择坐标系范围,绘制出合适的二维曲线。如果给出了全部参数,则绘制出三维图形。

axis函数的功能丰富,其常用的用法有:
axis equal :纵横坐标轴采用等长刻度
axis square:产生正方形坐标系(默认为矩形)
axis auto:使用默认设置
axis off:取消坐标轴
axis on :显示坐标轴

Here Insert Picture Description
还有:给坐标加网格线可以用grid命令来控制,grid on/off命令控制画还是不画网格线,不带参数的grid命令在两种之间进行切换。
给坐标加边框用box命令控制。和grid一样用法
例 :绘制分段函数,并添加图形标注。(略)

3. 图形保持

一般情况下,每执行一次绘图命令,就刷新一次当前图形窗口,图形窗口原有图形将不复存在,如果希望在已经存在的图形上再继续添加新的图形,可以使用图形保持命令hold。hold on/off 命令是保持原有图形还是刷新原有图形,不带参数的hold命令在两者之间进行切换。
例:(略)

4. 图形窗口分割

在实际应用中,经常需要在一个图形窗口中绘制若干个独立的图形,这就需要对图形窗口进行分割。分割后的图形窗口由若干个绘图区组成,每一个绘图区可以建立独立的坐标系并绘制图形。同一图形窗口下的不同图形称为子图。Matlab提供了subplot函数用来将当前窗口分割成若干个绘图区,每个区域代表一个独立的子图,也是一个独立的坐标系,可以通过subplot函数激活某一区,该区为活动区,所发出的绘图命令都是作用于该活动区域。调用格式:

subplot(m,n,p)

该函数把当前窗口分成m×n个绘图区,m行,每行n个绘图区,区号按行优先编号。其中第p个区为当前活动区。每一个绘图区允许以不同的坐标系单独绘制图形。
例:(略)

三.绘制二维图形的其他函数

1. 其他形式的线性直角坐标图

在线性直角坐标中,其他形式的图形有条形图、阶梯图、杆图和填充图等,所采用的函数分别为:

bar(x,y,选项)      选项在单引号中
stairs(x,y,选项)
stem(x,y,选项)
fill(x1,y1,选项1,x2,y2,选项2,…)

前三个函数和plot的用法相似,只是没有多输入变量形式。fill函数按向量元素下标渐增次序依次用直线段连接x,y对应元素定义的数据点。

例5-8:分别以条形图、填充图、阶梯图和杆图形式绘制曲线

x=0:0.35:7;
y=2*exp(-0.5*x);
subplot(2,2,1);bar(x,y,'g');
title('bar(x,y,''g'')');axis([0, 7, 0 ,2]);
subplot(2,2,2);fill(x,y,'r');
title('fill(x,y,''r'')');axis([0, 7, 0 ,2]);
subplot(2,2,3);stairs(x,y,'b');
title('stairs(x,y,''b'')');axis([0, 7, 0 ,2]);
subplot(2,2,4);stem(x,y,'k');
title('stem(x,y,''k'')');axis([0, 7, 0 ,2]);

Here Insert Picture Description

2. 极坐标图

polar函数用来绘制极坐标图,调用格式为:
polar(theta,rho,选项)

其中,theta为极坐标极角,rho为极径,选项的内容和plot函数相似。

例5-9:绘制 的极坐标图

theta=0:0.01:2*pi;
rho=sin(3*theta).*cos(5*theta);
polar(theta,rho,'r');

3. 对数坐标图

在实际应用中,经常用到对数坐标,Matlab提供了绘制对数和半对数坐标曲线的函数,其调用格式为:

semilogx(x1,y1,选项1,x2,y2,选项2,…)
semilogy(x1,y1,选项1,x2,y2,选项2,…)
loglog(x1,y1,选项1,x2,y2,选项2,…)

这些函数中选项的定义和plot函数完全一样,所不同的是坐标轴的选取。semilogx函数使用半对数坐标,x轴为常用对数刻度,而y轴仍保持线性刻度。semilogy恰好和semilogx相反。loglog函数使用全对数坐标,x、y轴均采用对数刻度。
例:略
4. 对函数自适应采样的绘图函数
5. 其他形式的二维图形

二. 三维绘图

一.绘制三维曲线的基本函数

最基本的三维图形函数为plot3,它将二维绘图函数plot的有关功能扩展到三维空间,可以用来绘制三维曲线。其调用格式为:

plot3(x1,y1,z1,选项1,x2,y2,z2,选项2,…)

其中每一组x,y,z组成一组曲线的坐标参数,选项的定义和plot的选项一样。当x,y,z是同维向量时,则x,y,z对应元素构成一条三维曲线。当x,y,z是同维矩阵时,则以x,y,z对应列元素绘制三维曲线,曲线条数等于矩阵的列数。

例513 绘制空间曲线

该曲线对应的参数方程为

t=0:pi/50:2*pi;
x=8*cos(t);
y=4*sqrt(2)*sin(t);
z=-4*sqrt(2)*sin(t);
plot3(x,y,z,'p');
title('Line in 3-D Space');
text(0,0,0,'origin');
xlabel('X');ylabel('Y');zlabel('Z');grid;

二.三维曲面

1.平面网格坐标矩阵的生成

当绘制z=f(x,y)所代表的三维曲面图时,先要在xy平面选定一矩形区域,假定矩形区域为D=[a,b]×[c,d],然后将[a,b]在x方向分成m份,将[c,d]在y方向分成n份,由各划分点做平行轴的直线,把区域D分成m×n个小矩形。生成代表每一个小矩形顶点坐标的平面网格坐标矩阵,最后利用有关函数绘图。
产生平面区域内的网格坐标矩阵有两种方法:
利用矩阵运算生成。

x=a:dx:b;
y=(c:dy:d)’;
X=ones(size(y))*x;
Y=y*ones(size(x));

经过上述语句执行后,矩阵X的每一行都是向量x,行数等于向量y的元素个数,矩阵Y的每一列都是向量y,列数等于向量x的元素个数。
利用meshgrid函数生成;

x=a:dx:b;
y=c:dy:d;
[X,Y]=meshgrid(x,y);

语句执行后,所得到的网格坐标矩阵和上法,相同,当x=y时,可以写成meshgrid(x)

2.绘制三维曲面的函数

Matlab提供了mesh函数和surf函数来绘制三维曲面图。mesh函数用来绘制三维网格图,而surf用来绘制三维曲面图,各线条之间的补面用颜色填充。其调用格式为:

mesh(x,y,z,c)
surf(x,y,z,c)

一般情况下,x,y,z是维数相同的矩阵,x,y是网格坐标矩阵,z是网格点上的高度矩阵,c用于指定在不同高度下的颜色范围。c省略时,Matlab认为c=z,也即颜色的设定是正比于图形的高度的。这样就可以得到层次分明的三维图形。当x,y省略时,把z矩阵的列下标当作x轴的坐标,把z矩阵的行下标当作y轴的坐标,然后绘制三维图形。当x,y是向量时,要求x的长度必须等于z矩阵的列,y的长度必须等于必须等于z的行,x,y向量元素的组合构成网格点的x,y坐标,z坐标则取自z矩阵,然后绘制三维曲线。

例515 用三维曲面图表现函数 :

为了便于分析三维曲面的各种特征,下面画出3种不同形式的曲面。

%program 1
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
mesh(x,y,z);
xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');
title('mesh'); pause;
%program 2
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
surf(x,y,z);
xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');
title('surf'); pause;
%program 3
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
plot3(x,y,z);
xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');
title('plot3-1');grid;

程序执行结果分别如上图所示。从图中可以发现,网格图(mesh)中线条有颜色,线条间补面无颜色。曲面图(surf)的线条都是黑色的,线条间补面有颜色。进一步观察,曲面图补面颜色和网格图线条颜色都是沿z轴变化的。用plot3 绘制的三维曲面实际上由三维曲线组合而成。可以分析plot(x’,y’,z’)所绘制的曲面的特征。
例516 绘制两个直径相等的圆管相交的图形。

m=30;
z=1.2*(0:m)/m;
r=ones(size(z));
theta=(0:m)/m*2*pi;
x1=r'*cos(theta);y1=r'*sin(theta);%生成第一个圆管的坐标矩阵
z1=z'*ones(1,m+1);
x=(-m:2:m)/m;
x2=x'*ones(1,m+1);y2=r'*cos(theta);%生成第一个圆管的坐标矩阵
z2=r'*sin(theta);
surf(x1,y1,z1);          %绘制竖立的圆管
axis equal ,axis off
hold on
surf(x2,y2,z2);          %绘制平放的圆管
axis equal ,axis off
title ('两个等直径圆管的交线');
hold off

例517 分析由函数 构成的曲面形状与平面z=a的交线。

此外,还有两个和mesh函数相似的函数,即带等高线的三维网格曲面函数meshc和带底座的三维网格曲面函数meshz,其用法和mesh类似。不同的是,meshc还在xy平面上绘制曲面在z轴方向的等高线,meshz还在xy平面上绘制曲面的底座。
surf函数也有两个类似的函数,即具有等高线的曲面函数surfc和具有光照效果的曲面函数surfl。

例518 在xy平面内选择[-8, 8]×[-8, 8]绘制函数

[x,y]=meshgrid(-8:0.5:8);
z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2+eps);
subplot(2,2,1);
meshc(x,y,z);
title('meshc');
subplot(2,2,2);
meshz(x,y,z);
title('meshz');
subplot(2,2,3);
surfc(x,y,z);
title('surfc');
subplot(2,2,4);
surfl(x,y,z);
title('surfl');

3.标准三维曲面

Matlab提供了一些函数用于绘制标准三维曲面,这些函数可以产生相应的绘图数据,常用于三维图形的演示。如,sphere函数和cylinder函数分别用于绘制三维球面和柱面。sphere函数的调用格式为:

[x,y,z]=sphere(n);

该函数将产生(n+1)×(n+1矩阵x,y,z 。采用这三个矩阵可以绘制出圆心位于原点、半径为1的单位球体。若在调用该函数时不带输出参数,则直接绘制所需球面。n决定了球面的圆滑程度,其默认值为20。若n值取的比较小,则绘制出多面体的表面图。
cylinder函数的调用格式为:

[x,y,z]=cylinder(R,n)

其中R是一个向量,存放柱面各个等间隔高度上的半径,n表示在圆柱圆周上有n个间隔点,默认有20个间隔点。如:cylinder(3)生成一个圆柱,cylinder([10,1])生成一个圆锥。而t=0:pi/100:4*pi; R=sin(t); cylinder(R,30);生成一个正弦圆柱面。
另外Matlab还提供了一个peaks函数,称为多峰函数,常用于三维曲面的演示。该函数可以用来生成绘图数据矩阵,矩阵元素由函数:
在矩形区域[-3 3]×[-3 3]的等分网格点上的函数值确定。如:z=peaks(30)
将生成一个30×30矩阵,

例519 绘制标准三维曲面图形

t=0:pi/20:2*pi;
[x,y,z]=cylinder(2+sin(t),30);
subplot(1,3,1);
surf(x,y,z);
subplot(1,3,2);
[x,y,z]=sphere;
surf(x,y,z);
subplot(1,3,3);
[x,y,z]=peaks(30);
meshz(x,y,z);

3.其他三维图形。

在介绍二维图形时,曾经提到条形图、杆图、饼图和填充图等特殊图形,它们还可以以三维形式出现,其函数分别为bar3,stem3,pie3和fill3。
bar3绘制三维条形图,常用格式为:

bar3(y);
bar3(x,y)

在第一种格式中,y的每个元素对应于一个条形。第二种格式在x指定的位置上绘制y中元素的条形图。
stem3函数绘制离散序列数据的三维杆图,常用格式为:

stem3(z)

stem3(x,y,z)

第一种格式将数据序列z表示为从xy平面向上延伸的杆图,x和y自动生成。第二种格式在x和y指定的位置上绘制数据序列z的杆图,x,y,z的维数要相同。
pie3函数绘制三维饼图,常用格式为:

pie3(x)

x为向量,用x中的数据绘制一个三维饼图。
fill3函数可在三维空间内绘制出填充过的多边形,常用格式为:

fill3(x,y,z,c)

用x,y,z做多边形的顶点,而c指定了填充的颜色。

例520 绘制三维图形。

1绘制魔方阵的三维条形图2以三维杆图形式绘制曲线y=2sinx 3已知x =[2347,1827,2043,3025] ,绘制三维饼图 4用随机的顶点坐标值画出5个黄色三角形

subplot(2,2,1);
bar3(magic(4));
subplot(2,2,2);
y=2*sin(0:pi/10:2*pi);
stem3(y);
subplot(2,2,3);
pie3([2347,1827,2043,3025]);
subplot(2,2,4);
fill3(rand(3,5),rand(3,5),rand(3,5),'y');

除了上面讨论的三维图形外,常用的图形还有瀑布图和三维曲面的等高线图。绘制瀑布图用waterfall函数,用法和meshz函数相似,只是它的网格线在x轴方向出现,具有瀑布效果。等高线图分二维和三维两种形式,分别使用函数contour和contour3绘制。

例521 绘制多峰函数的瀑布图和等高线图。

subplot(1,2,1);
[X,Y,Z]=peaks(30);
waterfall(X,Y,Z);
xlabel('XX');ylabel('YY');zlabel('ZZ');
subplot(1,2,2);
contour3(X,Y,Z,12,'k');%其中12代表高度的等级数
xlabel('XX');ylabel('YY');zlabel('ZZ');

三.三维图形的精细处理

一.视点处理

在日常生活中,从不同的角度观察物体,所看到的物体形状是不一样的。同样,从不同视点绘制的三维图形的形状也是不一样的。视点位置可由方位角和仰角表示。
方位角
Matlab提供了设置视点的函数view,其调用格式为:
view(az,el)
其中az为方位角,el为仰角,它们均以度为单位。系统默认的视点定义为方位角为-37.5度,仰角30度。

例522 从不同视点绘制多峰函数曲面。

subplot(2,2,1);mesh(peaks);
view(-37.5,30);
title('1');
subplot(2,2,2);mesh(peaks);
view(0,90);
title('2');
subplot(2,2,3);mesh(peaks);
view(90,0);
title('3');
subplot(2,2,4);mesh(peaks);
view(-7,-10);
title('4');

二.色彩处理

三.图形的裁剪处理

Matlab定义的NaN常数可以用于表示那些不可使用的数据,利用这些特性,可以将图形中需要裁剪部分对应的函数值设置成NaN,这样在绘制图形时,函数值为NaN的部分将不显示出来,从而达到对图形进行裁剪的目的。例如,要削掉正弦波顶部或底部大于0.5的部分,可使用下面的程序。

x=0:pi/10:4*pi;
y=sin(x);
i=find(abs(y)>0.5);
x(i)=NaN;
plot(x,y);

Example 524 Draw two spheres, one within the other, the outside of the cut part of the ball, in order to see the inside of the ball.

[x,y,z]=sphere(25);
%生成外面的大球
z1=z;
z1(:,1:4)=NaN;%将大球裁去一部分
c1=ones(size(z1));
surf(3*x,3*y,3*z1,c1); %生成里面的小球
hold on
z2=z;
c2=2*ones(size(z2));
c2(:,1:4)=3*ones(size(c2(:,1:4)));
surf(1.5*x,1.5*y,1.5*z2,c2);
colormap([0 1 0;0.5 0 0;1 0 0]);
grid on
hold off

FIG color using three colors, green is outside the ball, two kinds of different shades of red ball inside employed.

four. Implicit function graphing

Given the explicit expression functions, you can set the vector argument, and then according to the expression vector calculation function, so as to draw a graphical plot other functions. However, when the function takes the form of implicit functions, such as: it is difficult to draw graphics by the above method. Matlab provides a ezplot function draws an implicit function graph. Usage is as follows:
① to function f=f(x), ezplotcall the format:

ezplot(f),在默认区间(-2pi,2pi)绘制图形。
ezplot(f,[a,b]),在区间(a,b)绘制

② For implicit function f=f(x,y), ezplotcall the format;

ezplot(f),在默认区间(-2pi,2pi),(-2pi,2pi)绘制f(x,y)=0的图形。
ezplot(f,[xmin,xmax,ymin,ymax]);在区间          绘制图形。
ezplot(f,[a,b]),在区间(a,b),(a,b)绘制

For parameter equation ③ x=x(t), y=y(t), ezplotfunction call format is:

ezplot(x,y),在默认区间 绘制x=x(t),y=y(t)图形。
ezplot(x,y,[tmin,tmax]),在区间(tmin,tmax)绘制x=x(t),y=y(t)图形。

Example 525 Example drawing implicit function.

subplot(2,2,1);
ezplot('x^2+y^2-9');axis equal;
subplot(2,2,2);
ezplot('x^3+y^3-5*x*y+1/5')
subplot(2,2,3);
ezplot('cos(tan(pi*x))',[0,1]);
subplot(2,2,4);
ezplot('8*cos(t)','4*sqrt(2)*sin(t)',[0,2*pi]);

There are other implicit function ezpolardrawing, ezcontour, ezplot3, ezmesh, ezmeshc, ezsurf, ezsurfc, .

Guess you like

Origin blog.csdn.net/nuoyanli/article/details/93307143