Drawing function in Matlab

Table of contents

1. Two-dimensional curves and graphics

1. Two-dimensional image basic command plot

(1). Curve line style, color and marker type

(2). Set the curve line width, marker size, marker border color and marker fill color, etc.

(3). Coordinate axis setting

(4). Coordinate axis scale setting

(5). Legend

(6). More settings

2. Graphics control and performance 

1. graphics window

(1). Creation and selection of graphics window

(2). Draw multiple sub-graphics in one graphics window

(3). Drawing on an existing graph

2. Coordinate axis control command

(1) Range of coordinate axes

(2) The influence of the display scale on the drawing result

3. Graphic annotation

(1). Add coordinate axis identification and graph title

(2). Add text in the figure

(3). Specify TeX characters

(4). Add a legend frame in the graph

4. Graph style and color

(1). The form of the graph

(2). Line color

(3). Other properties of the graph line


    When doing Matlab programming before, drawing is always a very important part. Here we sort out the commonly used drawing functions for backup.


1. Two-dimensional curves and graphics

    MATLAB provides a variety of drawing commands for two-dimensional graphics

command name Meaning and function
area Area map; mainly used to express proportion and composition
bar Histogram; mainly used for statistical data
compass Ray diagram; mainly used for direction and velocity
feather Feather plot; mainly used for speed
hist Frequency histogram; mainly used for statistics
pie Two-dimensional pie chart; statistical data in polar form
plot Basic 2D Curve Graphics Commands
polar draw a curve in polar coordinates
quiver Two-dimensional arrow diagram; mainly used for field strength, flow direction
rose Frequency fan chart; mainly used for statistics
stairs Staircase diagram; mainly used for sampling data
stem 2-D bar chart; primarily useful for discrete data

1. Two-dimensional image basic command plot

    Basic calling format plot(x,y,'s')

    This is the most classic and basic calling format of the plot command. The input quantity (x, y, 's') to this command is called a planar plot triple. They respectively specify the geometric position, point shape, line type and color of the plane curve.

(1). Curve line style, color and marker type


    plot(X1,Y1,LineSpec, …) specifies the line type, color and mark type of data points of the curve through the string LineSpec.

symbol significance
- solid line
r red
+ plus
-. Dotted line
g green
o the circle
-- dotted line
b blue
* Asterisk
Dotted line
c blue-green
. point
m magenta
x cross sign
y yellow
s checkered
k black
d diamond
w White
^ upward triangle
> triangle to the left

<       right facing triangle 
p pentagon
h hexagon


 

(2). Set the curve line width, marker size, marker border color and marker fill color, etc.

plot(…,’Property Name’, Property Value, …)

Property Name significance options
LineWidth line width Such as 0.5, 1, etc., the unit is points
MarkerEdgeColor Marker border line color Color characters, such as 'g', 'b', etc.
MarkerFaceColor Marker point internal area fill color color characters
MarkerSize Marker size Value, the unit is points

(3). Coordinate axis setting


Range setting:
a. axis([xmin xmax ymin ymax]) sets the coordinate axis in the specified interval
b. axis auto sets the coordinate axis range of the current drawing area to the range automatically adjusted by MATLAB
c. axis manual freezes the current coordinate axis range, In the future, superimposed plots will be displayed within the range of the current coordinate axis
d. axis tight adopts the tight mode to set the current coordinate axis range, that is, a user data range is the coordinate axis range


Scale setting:
a. axis equal equal -proportion coordinate axis
b. axis square adjusts the coordinate axis area to a square shape based on the current coordinate axis range
c. axis normal automatically adjusts the vertical and horizontal axis ratios to make the graphics within the current coordinate axis range show the best


Range options and scale settings can be used in combination, the default setting is axis auto normal

(4). Coordinate axis scale setting


set(gca, ’XTick’, [0 1 2]) X坐标轴刻度数据点位置
set(gca,'XTickLabel',{'a','b','c'}) X坐标轴刻度处显示的字符
set(gca,'FontName','Times New Roman','FontSize',14) 设置坐标轴刻度字体名称,大小
‘FontWeight’,’bold’ 加粗 ‘FontAngle’,’italic’ 斜体


对字体的设置也可以用在title, xlabel, ylabel等中

(5). 图例


legend('a','Location','best') 图例位置放在最佳位置

用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表B的运动情况。

legend函数的基本用法是

legend(string1,string2,string3, ...)

分别将字符串1、字符串2、字符串3……标注到图中,每个字符串对应的图标为画图时的图标。

例如:

plot(x,sin(x),'.b',x,cos(x),'+r')

legend('sin','cos')这样可以把"."标识为'sin',把"+"标识为"cos"

还可以用legend(...,'Location',LOC) 来指定图例标识框的位置

这些是Matlab help文件。后面一段是对应的翻译和说明:

参数 英文说明 中文说明

'North'      

inside plot box near top 图例标识放在图顶端
'South' inside bottom 图例标识放在图底端
'East' inside right 图例标识放在图右方
'West' inside left 图例标识放在图左方

'NorthEast'

inside top right (default) 图例标识放在图右上方(默认)
'NorthWest           inside top left 图例标识放在图左上方
'SouthEast' inside bottom right 图例标识放在图右下角
'SouthWest' inside bottom left 图例标识放在图左下角
'NorthOutside'       outside plot box near top 图例标识放在图框外侧上方
'SouthOutside'       outside bottom 图例标识放在图框外侧下方
'EastOutside'         outside right 图例标识放在图框外侧右方
'WestOutside'         outside left 图例标识放在图框外侧左方
'NorthEastOutside'   outside top right 图例标识放在图框外侧右上方
'NorthWestOutside'   outside top left 图例标识放在图框外侧左上方
'SouthEastOutside'   outside bottom right 图例标识放在图框外侧右下方
'SouthWestOutside'   outside bottom left 图例标识放在图框外侧左下方
'Best'               least conflict with data in plot 图标标识放在图框内不与图冲突的最佳位置
'BestOutside'        least unused space outside plot 图标标识放在图框外使用最小空间的最佳位置

还是用上面的例子

legend('sin','cos','location','northwest')可以将标识框放置在图的左上角。

示例代码:

x = 0:.2:12;

plot(x,sin(x),x,cos(x),x,tan(x));

legend('First','Second','Third');

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')

x = -pi:pi/10:pi;

y = tan(sin(x)) - sin(tan(x));

plot(x,y,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)

xlabel('x');

ylabel('y');

·   

(6). 更多的设置

可以在绘图窗口中打开绘图工具,Inspector… 中查找

二、 图形的控制与表现 

MATLAB提供的用于图形控制的函数和命令:
axis: 人工选择坐标轴尺寸.
clf:清图形窗口.
ginput: 利用鼠标的十字准线输入.
hold: 保持图形.
shg:显示图形窗口.
subplot: 将图形窗口分成N块子窗口。


1.图形窗口


(1). 图形窗口的创建和选择


figure(n):用于为当前的绘图创建图形窗口,每运行一次figure就会创建一个新的图形窗口,n表示第n个窗口,如果窗口定义了句柄,也可以用figure(h)将句柄h的窗口作为当前窗口。
clf:用于清除当前图形窗口中的内容。
shg:用于显示当前图形窗口。


(2). 在一个图形窗口中绘制多个子图形


subplot(m,n,p):把窗口分成m×n个小窗口,并把第p个窗口当作当前窗口。
例:将4 个图形显示在同一个图形窗口中。

t=0:pi/20:2*pi; 
[x,y]=meshgrid(t);
subplot(2,2,1); 
plot(sin(t),cos(t)); 
axis equal
subplot(2,2,2); 
z=sin(x)+cos(y); 
plot(t,z); 
subplot(2,2,3); 
z=sin(x).*cos(y); 
plot(t,z); 
subplot(2,2,4); 
z=sin(x).^2-cos(y).^2; 
plot(t,z); 


(3). 在一个已有的图形上绘图

 
hold on:在一个已有的图形上继续绘图;

hold off: 命令结束继续绘图。
例:将peaks函数的等高线图与伪彩色画在一起。

[x,y,z]=peaks;          %产生双变量数组
contour(x,y,z,20,'k')        %绘制等高线
hold on
pcolor(x,y,z)             %绘制伪彩色图
shading interp          %表面色彩渲染
hold off


 

2.坐标轴控制命令(Axis control commands)


   控制坐标性质的axis函数的多种调用格式:
axis(xmin xmax ymin ymax) :指定二维图形x和y轴的刻度范围,
axis auto                                 :设置坐标轴为自动刻度(缺省值)
axis manual(或axis(axis)) :保持刻度不随数据的大小而变化
axis tight                                 :以数据的大小为坐标轴的范围
axis ij                                       :设置坐标轴的原点在左上角,i为纵坐标,j为横坐标
axis xy                                     :使坐标轴回到直角坐标系
axis equal                                :使坐标轴刻度增量相同
axis square                              :使各坐标轴长度相同,但刻度增量未必相同
axis normal                              :自动调节轴与数据的外表比例,使其他设置失效
axis off                                     :使坐标轴消隐
axis on                                     :显现坐标轴


(1) 坐标轴的范围


二维图形坐标轴范围在缺省状态下是根据数据的大小自动设置的,如欲改变,可利用axis(xmin xmax ymin ymax)函数来定义。
例: 定义坐标轴范围对观察图形的影响。

x=0:.01:pi/2; 
figure(1); 
plot(x,tan(x),'-ro') %ymax=tan(1.57),而其他数据都很小,结果将使图形难于进行观察和判断。
figure(2); 
plot(x,tan(x),'-ro'); 
axis([0, pi/2,0,5]) %对坐标轴的范围进行控制就可得到较满意的绘图结果


(2) 显示比例对绘图结果的影响(Effect of display scaling on plotting results)

例:比较(Default, axis square, axis equal, axis tight)几种不同的显示方式的显示效果。
 

t=0:pi/20:2*pi; figure(1);
subplot(2,1,1); plot(sin(t),2*cos(t)); grid on     %缺省状态下的图形比例
subplot(2,1,2); plot(sin(t),2*cos(t)); axis square; grid on    %正方形的显示比例
figure(2)
subplot(1,2,1); plot(sin(t),2*cos(t)) ; axis equal; grid on           %具有相等的刻度比例
subplot(1,2,2); plot(sin(t),2*cos(t)); axis tight ; grid on              %紧缩形式



3.图形标注


title     :标题,
xlabel :x轴标注,
ylabel :y轴标注,
text     :任意定位的标注                      
gtext   :鼠标定位标注,
legent :标注图例


图形标注可以使用字母,数字,汉字或按规定的方法表示希腊字母。如:pi表示πleq表示rm表示后面的字恢复为正体字,it表示斜体字,FontSize表示字体的大小, FontName表示字体的类型等。
可以使用图形窗口的Insert菜单,也可以使用属性编辑器,还可以使用函数输入的方法加标注,以下介绍相关函数的使用方法。


(1). 加注坐标轴标识和图形标题


加注坐标轴标识:xlabel(‘s’), ylabel(‘s’)
图形标题           :   title(‘s’)
例:加注坐标轴标示和图形标题。

t=0:pi/100:2*pi;y=sin(t);
plot(t,y)
axis([0 2*pi,-1 1])
xlabel('0 leq itt rm leq pi','FontSize',16)
ylabel('sin(t)','FontSize',20)
title('正弦函数图形','FontName','隶书','FontSize',20)


(2). 图中加注文本(Add text in the figure)
 

text(x,y,’字符串’)


例:在上图中加语句。
 

t=0:pi/100:2*pi;
y=sin(t);
plot(t,y)
axis([0 2*pi,-1 1])
xlabel('0 leq itt rm leq pi','FontSize',16)
ylabel('sin(t)','FontSize',20)
title('正弦函数图形','FontName','隶书','FontSize',20)
text(3*pi/4,sin(3*pi/4),'leftarrowsin(t)=0.707', 'FontSize',16)
text(pi,sin(pi),'leftarrowsin(t)=0', 'FontSize',16)
text(5*pi/4,sin(5*pi/4),'sin(t)=-0.707rightarrow','FontSize',16,HorizontalAlignment','right')


句中:
leftarrow           表示加一个向左的箭头
rightarrow          表示加一个向右的箭头
HorizontalAlignment 表示右对齐水平排列

 
gtext('字符串'): 在图形窗口上用鼠标直接在指定的位置上加注文本。

例:

t=0:pi/100:2*pi;
y=sin(t);
plot(t,y)
axis([0 2*pi,-1 1])
xlabel('0 leq itt rm leq pi','FontSize',16)
ylabel('sin(t)','FontSize',20)
title('正弦函数图形','FontName','隶书','FontSize',20)
gtext('MATLAB')


 

(3). 指定TeX字符


例:在标题中指定TeX字符
 

t=0:pi/100:2*pi;
alpha=-0.8;
beta=15;
y=sin(beta*t).*exp(alpha*t);
plot(t,y)
title('{itAe}^{-italphaitt}sinitbeta{itt}italpha<<itbeta')
xlabel('时间mus.'),
ylabel('幅值')


在title中的字符串表现的是 Aeαt   sinβt   α<<β
斜体Ae 上标斜体αt 斜体βt 斜体α  斜体β

 
(4). 在图形中添加图例框


legend(字符串1,字符串2,…)
例:在当前图形中添加图例说明。

x=0:pi/10:2*pi;
y1=sin(x);
y2=0.6*sin(x);
y3=0.3*sin(x);
plot(x,y1,x,y2,'-o',x,y3,'-*')
legend( '曲线1','曲线2','曲线3')
legend('boxoff')





4. 图线形式和颜色


(1). 图线的形式: (style of plot)

四种线形    : 实线'-',虚线'--', 点线':',点划线'-'.
标记点类型点'.', 圆'o', 加号'+', 星号'*', x符号'x', 方形's', 菱形'd', hexagram 'h',上三角△ '^', 下三角▽ 'v', 左三角'<', 右三角'>', 正五边形'p'

命令:plot(x,y,'—'), plot(x1,y1,':’,x2,y2,'*')
例1:选择不同的线形绘图。    

t=0:pi/100:2*pi; y=sin(t); y2=sin(t-0.25); y3=sin(t-0.5);
plot(t,y,'-',t,y2,'-',t,y3,':')



例2:选择不同的标记点绘图。

t=0:pi/20:2*pi; 
x=t.^3; 
y=sin(t); 
plot(x,y,'o')

 


(2). 线的颜色(color of plot)

可选颜色: 红r,绿g, 蓝b, 黄y, 粉红m, 青c, 黑k.
例:

t=0:pi/20:2*pi;
y=sin(t); 
plot(x,y,'r'), plot(x,y,'g+')


(3). 图线的其他属性(other characters of plot)

设置图线的宽度    : 'LineWidth'

标记点的边缘颜色: 'MarkerEdgeColor'

填充颜色              : 'MarkerFaceColor'

标记点的大小       : 'MarkerSize'

例: 设置图线的线形、颜色、宽度、标记点的颜色及大小。

t=0:pi/20:pi; 
y=sin(4*t).*sin(t)/2;
plot(t,y,'-bs','LineWidth',2,'MarkerEdgeColor','k', 'MarkerFaceColor', 'y','MarkerSize',10);

Guess you like

Origin blog.csdn.net/daijingxin/article/details/78116975
Recommended