Matlab绘图
数据可视化在数模论文中至关重要,一幅美观、清晰的图片往往会使论文增色不少。而Matlab提供了大量绘图函数,具有强大的绘图能力。利用Matlab作出的图形简洁、美观且可调节性高。因此,本文将介绍Matlab常用的绘图函数,并结合示例使图像美观。
一、Plot(二维线图)
- 语法: p l o t ( X 1 , Y 1 , L i n e S p e c 1 , . . . , X n , Y n , L i n e S p e c n ) plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn)
- 示例:
x = 0:pi/100:2*pi;
y = sin(x);
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3,'LineWidth',2)
legend('sin(x)','sin(x-.25)','sin(x-.5)')
xlabel('x','FontSize',14)
ylabel('y','FontSize',14)
title('Title','FontSize',16)
二、Scatter(散点图)
- 语法:
s
c
a
t
t
e
r
(
x
,
y
,
s
z
,
c
)
scatter(x,y,sz,c)
scatter(x,y,sz,c)
sz:散点尺寸
c:散点颜色 - 示例:
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],c,'filled')
三、Bar(柱状图)
- 语法: b a r ( x , y ) bar(x,y) bar(x,y)
- 示例:
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'FaceColor',"flat","EdgeColor","flat");
四、Pie(饼状图)
- 语法:
p
i
e
(
X
,
e
x
p
l
o
d
e
,
l
a
b
e
l
s
)
pie(X,explode,labels)
pie(X,explode,labels)
explode:偏移量
labels:文本标签 - 示例:
y2010 = [50 0 100 95];
y2011 = [65 22 97 120];
labels = {'Investments','Cash','Operations','Sales'};
t = tiledlayout(1,2,'TileSpacing','compact');
% Create pie charts
ax1 = nexttile;
p=pie(ax1,y2010);
title('2010')
ax2 = nexttile;
pie(ax2,y2011)
title('2011')
% Create legend
lgd = legend(labels);
五、Area(堆叠面积图)
- 语法; a r e a ( X , Y ) area(X,Y) area(X,Y)
- 示例:
x = 1:7;
data1=[120, 132, 101, 134, 90, 230, 210];
data2=[220, 182, 191, 234, 290, 330, 310];
data3= [150, 232, 201, 154, 190, 330, 410];
data4= [320, 332, 301, 334, 390, 330, 320];
data5= [820, 932, 901, 934, 1290, 1330, 1320];
a=area(x,[data1;data2;data3;data4;data5]');
newcolors = ['#80FFA5'; '#00DDFF'; '#37A2FF'; '#FF0087';'#FFBF00'];
colororder(newcolors)
for i=1:5
a(i).EdgeColor=newcolors(i,:);
end
legend
六、Surf(曲面图)
- 语法; s u r f ( X , Y , Z , C ) surf(X,Y,Z,C) surf(X,Y,Z,C)
- 示例:
[X,Y] = meshgrid(-5:.5:5);
Z = Y.*sin(X) - X.*cos(Y);
s = surf(X,Y,Z,'FaceAlpha',0.5);
s.EdgeColor = 'none';
colorbar