Usage of plot function in MATLAB

Use plot to draw a 2D image

This article is transferred from http://blog.sina.com.cn/s/blog_d8f783c90102woqb.html

and https://blog.csdn.net/alvern_zhang/article/details/51153058

The plot function in MATLAB is often used to draw various two-dimensional images, and its usage is also varied. This article only introduces the basic usage of the plot function - using the plot function to draw two-dimensional point and line graphs. The general calling form of the plot function is as follows:

plot(X, Y, LineSpec)

where X consists of the x values ​​of all input point coordinates and Y is a vector of y corresponding to the x contained in X. LineSpec is a user-specified drawing style, the main options are as follows:

Specifier

Line Style

-

--

Solid line (default style)

Dotted line (dash)

:

Dotted line

-.

Dotted line

 

Specifier

Marker (point style)

O

round

+

plus

*

Asterisk

.

point

x

cross

s

square

d

diamond

^

upward pointing triangle

v

downward pointing triangle

right pointing triangle

<<o:p> 

left pointing triangle

p

Pentagram

h

Hexagon

 

Specifier

Color

and

yellow

m

magenta

c

blue-green

r

Red

g

green

b

blue

w

白色

k

黑色

例如:’--or’表示坐标点为圆圈标志,且线型为红色短划线的绘图样式。

 

注意

在同时绘制多条曲线时,如果没有指定曲线属性,plot按顺序循环使用当前坐标系中ColorOrder和LineStyleOrder两个属性。

默认情况,MATLAB在每次调用plot函数时将ColorOrder和LineStyleOrder自动重置为DefaultAxesColorOrder和DefaultAxesLineStyleOrder。Default**属性我们可以自定义,有效期至MATLAB关闭,Matlab下次启动时将Default**属性重置为厂家设置(Factory)

set(0,’DefaultAxesColorOrder’,’r|g|b|k’,…’DefaultAxesLineStyleOrder’,’-|-.|–|:’)

使用hold all命令可以阻止调用plot函数时自动重置ColorOrder和LineStyleOrder属性,而是循环使用。注意hold on只是使多次绘制的图形叠加(相当于NextPlot),但不能阻止属性重置。

另外我们可以通过下面四个属性设置标识符的颜色和大小

LineWidth——指定线宽

MarkerEdgeColor——指定标识符的边缘颜色

MarkerFaceColor——指定标识符填充颜色

MarkerSize——指定标识符的大小

注意上面四个属性是针对当前坐标系中所有曲线的

实例

X=1:10; 
% 两个都是数组,必须具有相同的尺寸 
X1=[X;X;X]’;%10×3 
Y1=rand(10,3)+1;%10×3% 其中一个为向量,另一个为数组,自动匹配尺寸相等方向 
X2=1:0.1:10;%1×91 
Y2=[sin(X2);cos(X2)]’;%91×2% 其中一个是标量,另一为矢量,绘制垂直坐标轴的离散点 
X3=1:10; 
Y3=-0.5; 
fh=figure(‘numbertitle’,’off’,’name’,’PLOT Usability Demo’);%创建figure对象 
ah=axes;%创建axes对象 
h=plot(…%返回所有曲线句柄 
ah,…%指定坐标系,可以省略,此时默认gca 
X1,Y1,…%坐标数据 
‘-.^’,…%曲线属性,可以省略或部分省略,此时自动选择 
X2,Y2,… 
‘m-‘,… 
X3,Y3,… 
‘o’,…%注意此组数据设置线型和颜色无效,因为默认绘制离散点 
‘LineWidth’,2,…%线宽 
‘MarkerEdgeColor’,’k’,…%标识符边缘颜色 
‘MarkerFaceColor’,’r’,…%标识符填充颜色 
‘MarkerSize’,8)%标识符大小

这里写图片描述



举例如下:

定义x02pi之间的一组向量,且x向量中相邻两个值之间的增量为pi/100。定义y向量中的值为x向量的sine值。

x = 0:pi/100:2*pi;

y = sin(x);

figure 打开新的绘画窗口,可省略该句

plot(x,y) %采用默认样式,绘制实线

MATLAB中plot函数的简单用法

x = 0:pi/20:2*pi;

y = sin(x);

plot(x,y,'--or') %坐标点为圆圈标志,且线型为红色短划线的绘图样式

 MATLAB中plot函数的简单用法

注:可以根据上述LineSpec表格自由组合线型样式。

 

在不关闭绘图窗口的前提下,还可以为图像添加标题,x轴和y轴标签,例如:

xlabel('x')

ylabel('sin(x)')

title('Plot of the Sine Function')

MATLAB中plot函数的简单用法

 

想要在现有的图片上添加另外一条线,可以使用hold语句,例如:

x = 0:pi/100:2*pi;

y = sin(x);

plot(x,y)

hold on

y2 = cos(x);

plot(x,y2,'r:')

legend('sin','cos') %为图片添加图例

MATLAB中plot函数的简单用法



使用plot3绘制三维图像

MATLAB中有许多函数可以用来绘制三维图像,在此仅对plot3函数进行简要介绍,有兴趣的同学可以自行学习。plot3函数的调用形式如下:

plot3(X, Y, Z,LineSpec)

例如:

z=0:pi/50:10*pi;

x=sin(z);

y=cos(z);

plot3(x,y,z)

MATLAB中plot函数的简单用法

 

 


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326857708&siteId=291194637