MATLAB数据可视化分析

MATLAB 提供了强大的数据可视化功能,能够创建各种高质量图表。以下是一些常用的数据可视化方法和示例:

1. 基本绘图函数

% 生成示例数据
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);

% 基本折线图
figure;
plot(x, y1, 'b-', 'LineWidth', 1.5);  % 蓝色实线
hold on;
plot(x, y2, 'r--', 'LineWidth', 1.5); % 红色虚线
title('正弦和余弦函数');
xlabel('x');
ylabel('y');
legend('sin(x)', 'cos(x)');
grid on;
hold off;

2. 散点图

% 生成随机数据
x = randn(100,1);
y = randn(100,1);
z = x.^2 + y.^2;

% 散点图,点的大小和颜色随z值变化
figure;
scatter(x, y, 50*z, z, 'filled');
colorbar; % 显示颜色条
title('散点图示例');
xlabel('X');
ylabel('Y');

3. 柱状图

% 示例数据
categories = {'A', 'B', 'C', 'D', 'E'};
values1 = [3, 7, 2, 5, 8];
values2 = [2, 5, 1, 7, 4];

% 分组柱状图
figure;
bar([values1; values2]', 'grouped');
set(gca, 'XTickLabel', categories);
title('分组柱状图');
xlabel('类别');
ylabel('数值');
legend('数据1', '数据2');

4. 三维图形

% 生成网格数据
[X, Y] = meshgrid(-2:0.1:2);
Z = X .* exp(-X.^2 - Y.^2);

% 三维表面图
figure;
surf(X, Y, Z);
title('三维表面图');
xlabel('X');
ylabel('Y');
zlabel('Z');
colorbar;
shading interp; % 平滑着色

5. 子图

% 在一个窗口中创建多个子图
t = 0:0.1:10;

figure;

subplot(2,2,1); % 2行2列第1个
plot(t, sin(t));
title('sin(t)');

subplot(2,2,2); % 2行2列第2个
plot(t, cos(t));
title('cos(t)');

subplot(2,2,3); % 2行2列第3个
plot(t, tan(t));
title('tan(t)');
ylim([-5, 5]);

subplot(2,2,4); % 2行2列第4个
plot(t, exp(-0.1*t).*sin(t));
title('衰减正弦波');

这些示例展示了 MATLAB 中最常用的数据可视化方法。MATLAB 还支持更多高级图表类型,如等高线图、热图、饼图等,并且可以通过调整属性来美化图表的外观。你可以根据具体的数据类型和展示需求选择合适的可视化方式。

posted @ 2025-11-06 15:02  恰逢其时2008  阅读(6)  评论(0)    收藏  举报