图形标注 #

一、标题和标签 #

1.1 标题 #

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

% 基本标题
title('正弦函数');

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

% LaTeX公式
title('$y = \sin(x)$', 'Interpreter', 'latex');

% 设置属性
title('正弦函数', 'FontSize', 16, 'FontWeight', 'bold', 'Color', 'r');

% 获取标题句柄
h = title('正弦函数');
h.FontSize = 14;
h.Color = 'b';

1.2 坐标轴标签 #

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

% 基本标签
xlabel('x');
ylabel('sin(x)');

% 多行标签
xlabel({'时间', '(秒)'});
ylabel({'振幅', '(单位)'});

% LaTeX公式
xlabel('$x$ (弧度)', 'Interpreter', 'latex');
ylabel('$y = \sin(x)$', 'Interpreter', 'latex');

% 设置属性
xlabel('x', 'FontSize', 14, 'FontWeight', 'bold');
ylabel('y', 'FontSize', 14, 'Rotation', 0);  % 水平标签

1.3 z轴标签 #

matlab
[X, Y] = meshgrid(-2:0.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X, Y, Z);

zlabel('高度');

二、图例 #

2.1 基本图例 #

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

% 自动图例
legend show;
legend hide;
legend toggle;

2.2 图例位置 #

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

% 预定义位置
lgd.Location = 'north';
lgd.Location = 'south';
lgd.Location = 'east';
lgd.Location = 'west';
lgd.Location = 'northeast';    % 默认
lgd.Location = 'northwest';
lgd.Location = 'southeast';
lgd.Location = 'southwest';
lgd.Location = 'best';
lgd.Location = 'bestoutside';
lgd.Location = 'none';         % 手动定位

2.3 图例样式 #

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

% 方向
lgd.Orientation = 'vertical';    % 垂直
lgd.Orientation = 'horizontal';  % 水平

% 边框
lgd.Box = 'on';
lgd.EdgeColor = 'k';

% 背景
lgd.Color = [0.9 0.9 0.9];
lgd.FaceAlpha = 0.8;

% 字体
lgd.FontSize = 12;
lgd.FontName = 'Arial';

2.4 部分图例 #

matlab
x = 0:0.1:2*pi;
h1 = plot(x, sin(x), 'r-');
hold on;
h2 = plot(x, cos(x), 'b--');
h3 = plot(x, tan(x), 'g:');
hold off;

% 只显示部分图例
legend([h1, h2], 'sin(x)', 'cos(x)');

三、文本标注 #

3.1 text函数 #

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

% 基本文本
text(pi/2, 1, '最大值');

% 多行文本
text(pi, 0, {'零点', 'x = \pi'});

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

% 设置属性
text(pi/2, 0.5, '标注', ...
     'FontSize', 14, ...
     'Color', 'r', ...
     'FontWeight', 'bold', ...
     'BackgroundColor', 'w', ...
     'EdgeColor', 'k');

3.2 带箭头文本 #

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

% annotation函数
annotation('textarrow', [0.3 0.4], [0.7 0.8], ...
           'String', '最大值', ...
           'FontSize', 12);

% 使用textarrow
text(pi/2, 1.1, {'最大值', 'x = \pi/2'}, ...
     'HorizontalAlignment', 'center');

3.3 相对位置文本 #

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

% 使用数据单位
text(pi/2, 1, '最大值', 'Units', 'data');

% 使用归一化单位(0-1)
text(0.5, 0.5, '中心', 'Units', 'normalized');

四、注释 #

4.1 annotation函数 #

matlab
figure;

% 文本框
annotation('textbox', [0.2 0.5 0.3 0.1], ...
           'String', '这是一个文本框', ...
           'EdgeColor', 'r', ...
           'FitBoxToText', 'on');

% 箭头
annotation('arrow', [0.3 0.5], [0.4 0.6]);

% 双箭头
annotation('doublearrow', [0.5 0.7], [0.3 0.3]);

% 线条
annotation('line', [0.2 0.8], [0.2 0.2]);

% 矩形
annotation('rectangle', [0.3 0.3 0.4 0.3], ...
           'EdgeColor', 'b', ...
           'LineWidth', 2);

% 椭圆
annotation('ellipse', [0.3 0.3 0.4 0.3]);

4.2 文本箭头 #

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

% 文本箭头
annotation('textarrow', [0.3 0.4], [0.7 0.8], ...
           'String', '最大值', ...
           'FontSize', 12, ...
           'Color', 'r', ...
           'HeadStyle', 'plain');

4.3 文本框 #

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

% 文本框
annotation('textbox', [0.5 0.5 0.3 0.2], ...
           'String', {'说明:', '这是正弦函数曲线'}, ...
           'EdgeColor', 'k', ...
           'BackgroundColor', 'w', ...
           'FitBoxToText', 'on');

五、颜色条 #

5.1 基本颜色条 #

matlab
[X, Y] = meshgrid(-2:0.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X, Y, Z);
colorbar;

% 指定位置
colorbar('southoutside');
colorbar('eastoutside');  % 默认

5.2 颜色条标签 #

matlab
[X, Y] = meshgrid(-2:0.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X, Y, Z);
cb = colorbar;
cb.Label.String = '高度 (m)';

5.3 颜色条范围 #

matlab
[X, Y] = meshgrid(-2:0.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X, Y, Z);
colorbar;
caxis([-0.5 0.5]);  % 设置颜色范围

六、特殊标注 #

6.1 数据标签 #

matlab
x = 1:5;
y = [10 25 15 30 20];
bar(x, y);

% 添加数值标签
for i = 1:length(x)
    text(x(i), y(i)+1, num2str(y(i)), ...
         'HorizontalAlignment', 'center', ...
         'VerticalAlignment', 'bottom');
end

6.2 标记特殊点 #

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

% 找到最大值
[max_val, max_idx] = max(y);
hold on;
plot(x(max_idx), max_val, 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
text(x(max_idx), max_val+0.1, sprintf('最大值: %.2f', max_val), ...
     'HorizontalAlignment', 'center');
hold off;

6.3 区域标注 #

matlab
x = 0:0.1:10;
y = sin(x);
plot(x, y);
hold on;

% 填充区域
area_x = [2 2 4 4];
area_y = [-1 sin(2) sin(4) -1];
fill(area_x, area_y, 'r', 'FaceAlpha', 0.3);
text(3, 0, '关注区域', 'HorizontalAlignment', 'center');
hold off;

七、实用示例 #

7.1 完整标注示例 #

matlab
function plot_with_annotations(x, y)
    figure;
    plot(x, y, 'b-', 'LineWidth', 1.5);
    hold on;
    
    % 标题和标签
    title('数据分析结果', 'FontSize', 16, 'FontWeight', 'bold');
    xlabel('时间 (s)', 'FontSize', 12);
    ylabel('数值', 'FontSize', 12);
    
    % 网格
    grid on;
    
    % 标记最大值
    [max_val, max_idx] = max(y);
    plot(x(max_idx), max_val, 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
    text(x(max_idx), max_val, sprintf('  最大值: %.2f', max_val), ...
         'VerticalAlignment', 'bottom');
    
    % 标记最小值
    [min_val, min_idx] = min(y);
    plot(x(min_idx), min_val, 'go', 'MarkerSize', 10, 'MarkerFaceColor', 'g');
    text(x(min_idx), min_val, sprintf('  最小值: %.2f', min_val), ...
         'VerticalAlignment', 'top');
    
    % 添加说明框
    annotation('textbox', [0.7 0.7 0.2 0.15], ...
               'String', {sprintf('均值: %.2f', mean(y)), ...
                          sprintf('标准差: %.2f', std(y))}, ...
               'EdgeColor', 'k', ...
               'BackgroundColor', 'w');
    
    hold off;
end

八、总结 #

本章学习了:

  1. 标题和标签:title、xlabel、ylabel、zlabel
  2. 图例:legend、位置和样式设置
  3. 文本标注:text、LaTeX公式
  4. 注释:annotation、箭头、文本框
  5. 颜色条:colorbar
  6. 特殊标注:数据标签、标记特殊点

下一章将学习数据分析基础。

最后更新:2026-03-27