matlab数据展示技巧
Command
clear;
close all; % 关闭所有图形窗口
clc; % 清除命令行窗口中的数据
clf; % 清除当前figure中的图形
图形规范
method | explain |
---|---|
legend('') | 添加图例 |
axis([-inf,inf,0,inf]) | 规定坐标系范围 |
grid | 添加轴网格线 |
pause() | 停顿时间(秒) |
subplot() | 控制子图 |
绘图函数概览
MATLAB二维绘图工具
MATLAB三维绘图工具
如何动态更新显示图形
f1 = figure;
set(0,'CurrentFigure',f1);
xx = linspace(0,10);
for i = 1:100
set(0,'CurrentFigure',f1);
clf;
plot(xx(1:i),sin(xx(1:i)),'k-');
grid;
pause(0.1);
end
![](https://i-blog.csdnimg.cn/blog_migrate/489b1aa33caa07ad1dbecbc77422e2ff.png)
plot
多条线展示
close all;
xx = [0:0.2:10];
figure;
hold on;
plot(xx,sin(xx),'bo');
plot(xx,cos(xx),'r*-');
plot(xx,zeros(1,length(xx)),'k--');
legend('sin','cos','0');
![](https://i-blog.csdnimg.cn/blog_migrate/ebaf2985b8ddb806d36552f83066c130.png)
figure;
a = zeros(6,20);
for i=1:5
a(i,:) = i;
end
hold on;
plot(a(1,:),'r-+');
plot(a(2,:),'g-o');
plot(a(3,:),'b-*');
plot(a(4,:),'k-.');
plot(a(5,:),'c-x');
plot(a(6,:),'y-p');
legend('0','1','2','3','4','5');
hold off;
title('example');
![](https://i-blog.csdnimg.cn/blog_migrate/b2c4738fa025a49cd287fa21e7622e1a.png)
画一个矩形
figure;
a = [-1,1,1,-1,-1;-1,-1,1,1,-1];
hold on;
plot(a(1,:),a(2,:),'--k');
hold off;
axis([-2,2,-2,2]);
![](https://i-blog.csdnimg.cn/blog_migrate/978b8d24111bc3da3c8d9119a00b8c04.png)
boxplot
box显示的分别为最小值、下四分位数、中位数、上四分位数、最大值
figure;
a = randn(5,10);
boxplot(a);
![](https://i-blog.csdnimg.cn/blog_migrate/18ace7455cd1be5487085466fb648e5f.png)
scatter
控制每个点的颜色
figure;
xx = rand(1000,1)*100;
yy = rand(1000,1)*100;
ind = randperm(1000);
color = zeros(1000,3);
color(ind(1:500), 1) = 1;
scatter(xx,yy,[],color,'.'); % []控制大小,这个参数位置必须占住,默认为36
grid;
![](https://i-blog.csdnimg.cn/blog_migrate/edd909be4954f29280e9ffc5abed9758.png)
直方图 bar
累加直方图
figure;
a = zeros(6,20);
for i=1:5
a(i,:) = i;
end
bar(a','stacked');
legend('0','1','2','3','4','5');
title('example');
![](https://i-blog.csdnimg.cn/blog_migrate/6f9932199e5ae6ff8a24c91b35501624.png)
数据分布 histogram
这个命令在调试的时候很方便啊