【matlab基础篇03】⼀⽂带你全⾯了解plot绘图函数的使⽤(超
详细+图⽂并茂)
快速⼊门matlab,系统地整理⼀遍,如何你和我⼀样是⼀个新⼿,那么此⽂很适合你;
⽂章⽬录
1 前⾔
如果你是和我⼀样的⼩⽩,强烈推荐看看这⾥,需要合理地利⽤官⽅的⽂档,通常我觉得官⽅⽂档是最好的,没有之⼀,在命令终端输
⼊help plot,可以看到详细的帮助⽂档;具体如下;
>> help plot
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, disconnected
line objects are created and plotted as discrete points vertically at
X.
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
w    white        v    triangle (down)
^    triangle (up)
<    triangle (left)
>    triangle (right)
p    pentagram
h    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
solid yellow line interpolating green circles at the data points.
The plot command, if no color is specified, makes automatic use of
the colors specified by the axes ColorOrder property.  By default,
plot cycles through the colors in the ColorOrder property.  For
monochrome systems, plot cycles over the axes LineStyleOrder property.
Note that RGB colors in the ColorOrder property may differ from
similarly-named colors in the (X,Y,S) triples.  For example, the
second axes ColorOrder property is medium green with RGB [0 .5 0],
while plot(X,Y,'g') plots a green line with RGB [0 1 0].
If you do not specify a marker type, plot uses no marker.
If you do not specify a line style, plot uses a solid line.
plot(AX,...) plots into the axes with handle AX.
plot returns a column vector of handles to lineseries objects, one
handle per plotted line.
The X,Y pairs, or X,Y,S triples, can be followed by
parameter/value pairs to specify additional properties
of the lines. For example, plot(X,Y,'LineWidth',2,'Color',[.6 0 0])
will create a plot with a dark red line width of 2 points.
Example
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
与plot相关的函数还有plottools, semilogx, semilogy, loglog, plotyy, plot3, grid,title, xlabel, ylabel, axis, axes, hold, legend, subplot, scatter.
2 plot
2.1 显⽰正弦波
显⽰⼀个简单的正弦函数;
x=0:2*pi/100:2*pi;
y=sin(x);
plot(x,y);
2.2 修改颜⾊
参数颜⾊
b
g
r
c
m
y
k
w
下⾯修改为红⾊:
x=0:2*pi/100:2*pi;
y=sin(x);
plot(x,y,'r');
结果如下:
2.3 修改点的形状
参数形状图标
-solid
o circle
x x-mark
+plus
*star
s square
d diamond
v triangle (down)
^triangle (up)<triangle (left)
>
triangle (right)p
pentagram h hexagram
参数形状
图标将点形状显⽰为六边形;
x=0:2*pi/20:2*pi;
y=sin(x);
plot(x,y,'h','MarkerSize',10);结果如下:相关参数:
MarkerEdgeColor :点边框颜⾊;
MarkerFaceColor :点表⾯颜⾊;
MarkerSize :点的⼤⼩;
2.4 修改线的形状
符号
形状:
dotted -.
dashdot --dashed
x=0:2*pi/20:2*pi;
y=sin(x);
plot(x,y,':','LineWidth',3);LineWidth 的参数为线宽;
x=0:2*pi/20:2*pi;y=sin(x);
plot(x,y,'-.','LineWidth',3);
x=0:2*pi/20:2*pi;y=sin(x);
plot(x,y,'--','LineWidth',3);
2.5 多个参数修改
下⾯修改多个参数属性显⽰⼀下正弦波;
x = 0:2*pi/100:2*pi;
y = sin(x);
plot(x,y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...                'MarkerSize',10);
结果如下:
3 subplot
subplot 的使⽤⽅法如下:
subplot Create axes in tiled positions.
H = subplot(m,n,p), or subplot(mnp), breaks the Figure window
into an m-by-n matrix of small axes, selects the p-th axes for
the current plot, and returns the axes handle.  The axes are
counted along the top row of the Figure window, then the second    row, etc.  For example,
subplot(2,1,1), PLOT(income)
subplot(2,1,2), PLOT(outgo)
通俗的讲:
subplot(⾏,列,index)
注意:plot函数要在subplot表明位置之后再调⽤。
3.1 2⾏1列
x=0:2*pi/20:2*pi;
y=sin(x);
subplot(2,1,1);
plot(x,y,'y','LineWidth',3);
subplot(2,1,2);
plot(x,y,'g','LineWidth',3);
需要将多个波形显⽰在同⼀张图中;
3.2 1⾏2列
x=0:2*pi/20:2*pi;
y=sin(x);
subplot(1,2,1);
plot(x,y,'y','LineWidth',3);
subplot(1,2,2);
plot(x,y,'g','LineWidth',3);
4 plot3
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
5 title
title:图的标题;
xlabel:x轴标题;
ylabel:y轴标题;
指定plot的标题,需要在plot调⽤之后在调⽤title,xlabel或ylabel;
x=0:2*pi/20:2*pi;
y=sin(x);
subplot(1,2,1);
plot(x,y,'y','LineWidth',3);
title('yellow');
xlabel('yellow-x');
ylabel('yellow-y');
subplot(1,2,2);
plot(x,y,'g','LineWidth',3);
title('green');
xlabel('green-x');
ylabel('green-y');
用subplot函数6 legend
x = 0:.2:12;
plot(x,besselj(1,x),x,besselj(2,x),x,besselj(3,x));
legend('First','Second','Third','Location','NorthEastOutside')
b = bar(rand(10,5),'stacked'); colormap(summer); hold on
x = plot(1:10,5*rand(10,1),'marker','square','markersize',12,...
'markeredgecolor','y','markerfacecolor',[.6 0 .6],...
'linestyle','-','color','r','linewidth',2); hold off
legend([b,x],'Carrots','Peas','Peppers','Green Beans',...
'Cucumbers','Eggplant')
这是官⽅的demo,⽐较复杂;
x=0:2*pi/20:2*pi;
y=sin(x);
plot(x,y,':','LineWidth',3);
legend('test1');
legend需要在plot之后调⽤,⽤于依次解释第⼀个plot的波形,如果⼀个plot⾥显⽰了两个波形,那legen中字符串也需要设置两个,分别依次对应plot中的波形;
7 at last
⽐较简单,matplotlib的功能和这个⽐较类似,总体来说,作为⼀个和博主⼀样的新⼿,要多看官⽅的help⽂档,然后平时使⽤的过程中慢慢就熟悉了,最后要多总结。
作者能⼒有限,⽂中难免有错误和纰漏之处,请⼤佬们不吝赐教
创作不易,如果本⽂帮到了您;
请帮忙点个赞    ;
请帮忙点个赞    ;
请帮忙点个赞    ;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。