二维绘图 #

一、基本绘图 #

1.1 plot函数 #

matlab
% 基本折线图
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);

% 多条曲线
x = 0:0.1:2*pi;
plot(x, sin(x), x, cos(x));

% 使用句柄设置属性
h = plot(x, y);
h.LineWidth = 2;
h.Color = 'r';

1.2 线型和标记 #

matlab
x = 0:0.5:10;
y = sin(x);

% 线型: - 实线, -- 虚线, : 点线, -. 点划线
% 标记: o 圆, s 方块, d 菱形, ^ 三角, * 星号, + 加号, x 叉号
% 颜色: r红, g绿, b蓝, c青, m品红, y黄, k黑, w白

plot(x, y, 'r--o');        % 红色虚线,圆标记
plot(x, y, 'b-s', 'LineWidth', 2, 'MarkerSize', 10);
plot(x, y, '-o', 'Color', [0.5 0.5 0.5], 'MarkerFaceColor', 'r');

1.3 多图绘制 #

matlab
% 方法1:多次plot
x = 0:0.1:2*pi;
plot(x, sin(x), 'r-', x, cos(x), 'b--');
legend('sin(x)', 'cos(x)');

% 方法2:hold on
plot(x, sin(x), 'r-');
hold on;
plot(x, cos(x), 'b--');
hold off;
legend('sin(x)', 'cos(x)');

% 方法3:矩阵绘图
Y = [sin(x); cos(x)];
plot(x, Y);

二、散点图 #

2.1 scatter函数 #

matlab
% 基本散点图
x = randn(100, 1);
y = randn(100, 1);
scatter(x, y);

% 设置大小和颜色
sizes = rand(100, 1) * 100;
colors = rand(100, 1);
scatter(x, y, sizes, colors, 'filled');
colorbar;

% 指定颜色
scatter(x, y, 50, 'r', 'filled');
scatter(x, y, 50, [0.5 0.5 0.5], 'x');

2.2 plot散点 #

matlab
x = 1:10;
y = rand(1, 10);
plot(x, y, 'o');           % 只有标记
plot(x, y, '-o');          % 线和标记
plot(x, y, 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'b');

三、柱状图 #

3.1 bar函数 #

matlab
% 基本柱状图
x = 1:5;
y = [10 25 15 30 20];
bar(x, y);

% 水平柱状图
barh(x, y);

% 分组柱状图
Y = [10 20 15; 30 25 35; 20 15 25];
bar(Y);
legend('组1', '组2', '组3');

% 堆叠柱状图
bar(Y, 'stacked');

3.2 柱状图样式 #

matlab
y = [10 25 15 30 20];
b = bar(y);
b.FaceColor = 'flat';
b.CData(1,:) = [1 0 0];    % 红色
b.CData(2,:) = [0 1 0];    % 绿色
b.CData(3,:) = [0 0 1];    % 蓝色

四、其他二维图形 #

4.1 饼图 #

matlab
% 基本饼图
data = [30 20 25 15 10];
labels = {'A', 'B', 'C', 'D', 'E'};
pie(data, labels);

% 突出显示某部分
explode = [0 1 0 0 0];  % 突出第二块
pie(data, explode, labels);

% 三维饼图
pie3(data, labels);

4.2 直方图 #

matlab
% 基本直方图
data = randn(1000, 1);
histogram(data);

% 指定分组数
histogram(data, 30);

% 指定分组边界
histogram(data, -4:0.5:4);

% 归一化直方图
histogram(data, 'Normalization', 'probability');

4.3 面积图 #

matlab
% 基本面积图
x = 1:10;
y = cumsum(rand(1, 10));
area(x, y);

% 多系列面积图
Y = cumsum(rand(10, 3), 2);
area(Y);
legend('系列1', '系列2', '系列3');

4.4 填充图 #

matlab
% 填充多边形
x = [1 3 4 2 1];
y = [1 2 4 3 1];
fill(x, y, 'r');

% 带透明度
fill(x, y, 'r', 'FaceAlpha', 0.5);

4.5 极坐标图 #

matlab
% 极坐标折线图
theta = 0:0.1:2*pi;
r = sin(2*theta);
polarplot(theta, r);

% 极坐标散点图
polarscatter(theta, r);

% 极坐标柱状图
polarhistogram(theta);

4.6 阶梯图和火柴杆图 #

matlab
x = 0:10;
y = sin(x);

% 阶梯图
stairs(x, y);

% 火柴杆图
stem(x, y);
stem(x, y, 'filled', 'MarkerSize', 10);

五、图形标注 #

5.1 标题和标签 #

matlab
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);

title('正弦函数');
xlabel('x');
ylabel('sin(x)');

% 多行标题
title({'正弦函数', 'y = sin(x)'});

% 使用LaTeX
title('$y = \sin(x)$', 'Interpreter', 'latex');
xlabel('$x$ (弧度)', 'Interpreter', 'latex');

5.2 图例 #

matlab
x = 0:0.1:2*pi;
plot(x, sin(x), 'r-', x, cos(x), 'b--');
legend('sin(x)', 'cos(x)');

% 图例位置
legend('sin(x)', 'cos(x)', 'Location', 'northwest');
legend('sin(x)', 'cos(x)', 'Location', 'best');

% 图例方向
legend('sin(x)', 'cos(x)', 'Orientation', 'horizontal');

5.3 文本标注 #

matlab
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);

% 添加文本
text(pi/2, 1, '最大值');
text(3*pi/2, -1, '最小值');

% 带箭头的文本
text(pi/2, 0.8, {'最大值', 'x = \pi/2'}, 'Arrow', 'left');

% 使用LaTeX
text(pi/4, sin(pi/4), '$\sin(\pi/4) = \frac{\sqrt{2}}{2}$', ...
     'Interpreter', 'latex', 'FontSize', 12);

5.4 坐标轴设置 #

matlab
x = 0:0.1:10;
y = exp(x);
plot(x, y);

% 对数坐标轴
semilogy(x, y);  % y轴对数
semilogx(x, y);  % x轴对数
loglog(x, y);    % 双对数

% 设置坐标范围
xlim([0 5]);
ylim([0 100]);
axis([0 5 0 100]);

% 等比例坐标轴
axis equal;
axis tight;

% 显示网格
grid on;
grid minor;

六、实用示例 #

6.1 双Y轴图 #

matlab
x = 0:0.1:10;
y1 = sin(x);
y2 = exp(x/10);

figure;
yyaxis left;
plot(x, y1, 'b-');
ylabel('sin(x)');

yyaxis right;
plot(x, y2, 'r--');
ylabel('exp(x/10)');

xlabel('x');
legend('sin(x)', 'exp(x/10)');

6.2 误差条图 #

matlab
x = 1:5;
y = [10 20 15 25 18];
err = [2 3 2 4 3];

errorbar(x, y, err, 'o-', 'LineWidth', 2);
xlabel('组别');
ylabel('数值');
title('带误差条的柱状图');

6.3 热力图 #

matlab
data = rand(10, 10);
heatmap(data);
colormap('hot');
colorbar;
title('热力图');

七、总结 #

本章学习了:

  1. 基本绘图:plot函数、线型和标记
  2. 散点图:scatter函数
  3. 柱状图:bar、barh、堆叠柱状图
  4. 其他图形:饼图、直方图、面积图、极坐标图
  5. 图形标注:标题、标签、图例、文本
  6. 坐标轴设置:范围、对数、网格

下一章将学习三维绘图。

最后更新:2026-03-27