一、MATLAB 数据可视化概述
MATLAB 提供了丰富的二维和三维绘图函数,支持从简单的折线图到复杂的交互式可视化。其可视化系统基于 图形对象(Graphics Objects),包括图形窗口(Figure)、坐标轴(Axes)、线条(Line)、曲面(Surface)等,用户可以灵活控制每一个细节。
二、常用二维可视化函数
1. plot:基本线图
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y, 'r-', 'LineWidth', 2);
xlabel('x');
ylabel('sin(x)');
title('正弦函数图像');
grid on;
2. scatter:散点图
x = randn(100,1);
y = randn(100,1);
scatter(x, y, 50, y, 'filled'); % 颜色表示 y 值
colorbar;
3. bar / barh:柱状图
data = [23 45 56 78];
bar(data);
xticklabels({'A','B','C','D'});
4. histogram:直方图
data = randn(1000,1);
histogram(data, 30);
title('正态分布数据直方图');
5. pie:饼图
data = [30 25 15 30];
pie(data, {'A','B','C','D'});
6. errorbar:带误差棒的图
x = 1:5;
y = [2 4 3 5 6];
e = [0.5 0.3 0.4 0.2 0.6];
errorbar(x, y, e, 'o-');
三、三维可视化函数
1. plot3:三维线图
t = 0:pi/50:10*pi;
plot3(sin(t), cos(t), t);
xlabel('X'); ylabel('Y'); zlabel('Z');
title('三维螺旋线');
2. surf / mesh:曲面图
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-X.^2 - Y.^2);
surf(X,Y,Z);
colorbar;
title('二维高斯函数曲面');
shading interp; % 平滑着色
3. contour / contourf:等高线图
contour(X,Y,Z,20); % 20 条等高线
hold on;
plot3(sin(t), cos(t), zeros(size(t)), 'r'); % 叠加路径
4. scatter3:三维散点图
x = randn(100,1);
y = randn(100,1);
z = randn(100,1);
scatter3(x,y,z, 'filled');
四、高级可视化功能
1. 子图(Subplots)
subplot(2,2,1); plot(sin(x)); title('sin');
subplot(2,2,2); plot(cos(x)); title('cos');
subplot(2,2,3); plot(tan(x)); title('tan');
subplot(2,2,4); plot(exp(-x)); title('exp');
2. 多坐标轴(yyaxis)
yyaxis left
plot(x, sin(x), 'b');
ylabel('sin(x)');
yyaxis right
plot(x, 10*cos(x), 'r');
ylabel('10*cos(x)');
3. 动画与动态可视化
for t = 1:100
y = sin(x + t/10);
plot(x, y);
axis([0 2*pi -1 1]);
title(sprintf('帧 %d', t));
drawnow; % 实时刷新
end
4. 图像与地理可视化
imagesc:显示矩阵为彩色图像geoplot/geoscatter:地理数据绘图(需 Mapping Toolbox)
五、图形美化与定制
1. 线条与标记样式
plot(x, y, 'r--o', 'LineWidth', 2, 'MarkerSize', 6, 'MarkerFaceColor', 'g');
2. 字体与标签
set(gca, 'FontSize', 12, 'FontName', 'Times New Roman');
xlabel('时间 (s)', 'Interpreter', 'latex'); % 支持 LaTeX
3. 颜色映射(Colormap)
colormap jet; % 或 parula, hot, cool, gray 等
colorbar;
4. 图例(Legend)
legend('sin', 'cos', 'Location', 'best');
六、交互式可视化工具
- Figure 编辑模式:双击图形可进入编辑模式,直接修改属性。
- Plot Tools:使用
plottools命令打开图形编辑面板。 - App Designer:可构建带按钮、滑块等控件的交互式可视化应用。
七、导出与发布
saveas(gcf, 'myplot.png'); % 保存为 PNG
print(gcf, '-dpdf', 'report.pdf'); % 导出 PDF
exportgraphics(gcf, 'chart.jpg', 'Resolution', 300); % 高分辨率导出
推荐使用 exportgraphics 函数,支持更多格式和选项。
总结
MATLAB 的数据可视化系统强大而灵活,既能快速生成标准图表,也支持深度定制和交互式应用开发。掌握这些工具,可以帮助您更有效地分析和展示数据。
1万+

被折叠的 条评论
为什么被折叠?



