子图与布局 #

一、subplot函数 #

1.1 基本用法 #

matlab
% 2x2布局,选择第1个子图
subplot(2, 2, 1);
x = 0:0.1:2*pi;
plot(x, sin(x));
title('sin(x)');

% 选择第2个子图
subplot(2, 2, 2);
plot(x, cos(x));
title('cos(x)');

% 选择第3个子图
subplot(2, 2, 3);
plot(x, tan(x));
title('tan(x)');

% 选择第4个子图
subplot(2, 2, 4);
plot(x, exp(x));
title('exp(x)');

1.2 简化语法 #

matlab
% 使用三位数表示
subplot(221);  % 等同于 subplot(2, 2, 1)
subplot(222);  % 等同于 subplot(2, 2, 2)
subplot(223);  % 等同于 subplot(2, 2, 3)
subplot(224);  % 等同于 subplot(2, 2, 4)

1.3 不均匀布局 #

matlab
% 第一行占满
subplot(2, 1, 1);
x = 0:0.1:2*pi;
plot(x, sin(x));
title('sin(x)');

% 第二行分两列
subplot(2, 2, 3);
plot(x, cos(x));
title('cos(x)');

subplot(2, 2, 4);
plot(x, tan(x));
title('tan(x)');

1.4 跨越多个位置 #

matlab
% 第一个图跨越2列
subplot(2, 3, [1 2]);
x = 0:0.1:2*pi;
plot(x, sin(x));
title('sin(x)');

% 其他图
subplot(2, 3, 3);
plot(x, cos(x));
title('cos(x)');

subplot(2, 3, 4);
plot(x, tan(x));
title('tan(x)');

subplot(2, 3, 5);
plot(x, exp(x));
title('exp(x)');

subplot(2, 3, 6);
plot(x, log(x+1));
title('log(x+1)');

二、tiledlayout(R2019b+) #

2.1 基本用法 #

matlab
% 创建布局
t = tiledlayout(2, 2);

% 第一个图
nexttile;
x = 0:0.1:2*pi;
plot(x, sin(x));
title('sin(x)');

% 第二个图
nexttile;
plot(x, cos(x));
title('cos(x)');

% 第三个图
nexttile;
plot(x, tan(x));
title('tan(x)');

% 第四个图
nexttile;
plot(x, exp(x));
title('exp(x)');

% 总标题
title(t, '三角函数和指数函数');

2.2 布局间距 #

matlab
t = tiledlayout(2, 2);

% 设置间距
t.TileSpacing = 'compact';    % 紧凑
t.TileSpacing = 'loose';      % 宽松
t.TileSpacing = 'none';       % 无间距

% 设置边距
t.Padding = 'compact';
t.Padding = 'loose';
t.Padding = 'none';

2.3 跨越多个瓦片 #

matlab
t = tiledlayout(2, 3);

% 第一个图跨越2列
nexttile([1 2]);
x = 0:0.1:2*pi;
plot(x, sin(x));
title('sin(x)');

% 第二个图
nexttile;
plot(x, cos(x));
title('cos(x)');

% 第三个图跨越2行
nexttile([2 1]);
plot(x, tan(x));
title('tan(x)');

% 第四个图
nexttile;
plot(x, exp(x));
title('exp(x)');

2.4 流式布局 #

matlab
% 自动换行
t = tiledlayout('flow');

for i = 1:6
    nexttile;
    plot(rand(10, 1));
    title(sprintf('图%d', i));
end

三、axes函数 #

3.1 自定义位置 #

matlab
figure;

% 创建坐标轴,指定位置 [left bottom width height]
ax1 = axes('Position', [0.1 0.1 0.35 0.8]);
x = 0:0.1:2*pi;
plot(ax1, x, sin(x));
title(ax1, 'sin(x)');

ax2 = axes('Position', [0.55 0.1 0.35 0.8]);
plot(ax2, x, cos(x));
title(ax2, 'cos(x)');

3.2 嵌套坐标轴 #

matlab
figure;

% 主坐标轴
ax1 = axes;
x = 0:0.1:10;
plot(ax1, x, sin(x));
ax1.Color = 'none';  % 透明背景

% 嵌套坐标轴
ax2 = axes('Position', [0.6 0.6 0.25 0.25]);
plot(ax2, x, cos(x));

四、多图形窗口 #

4.1 创建多个窗口 #

matlab
% 第一个窗口
figure(1);
x = 0:0.1:2*pi;
plot(x, sin(x));
title('sin(x)');

% 第二个窗口
figure(2);
plot(x, cos(x));
title('cos(x)');

% 切换窗口
figure(1);  % 切换到第一个窗口

4.2 指定窗口属性 #

matlab
% 创建命名窗口
fig1 = figure('Name', '正弦函数', 'NumberTitle', 'off');
plot(x, sin(x));

fig2 = figure('Name', '余弦函数', 'NumberTitle', 'off');
plot(x, cos(x));

五、共享坐标轴 #

5.1 共享X轴 #

matlab
figure;

% 上图
ax1 = subplot(2, 1, 1);
x = 0:0.1:10;
plot(ax1, x, sin(x));
ylabel(ax1, 'sin(x)');

% 下图,共享X轴
ax2 = subplot(2, 1, 2);
plot(ax2, x, cos(x));
ylabel(ax2, 'cos(x)');

% 链接X轴
linkaxes([ax1, ax2], 'x');

5.2 共享Y轴 #

matlab
figure;

% 左图
ax1 = subplot(1, 2, 1);
y = 0:0.1:10;
plot(ax1, sin(y), y);
xlabel(ax1, 'sin(y)');

% 右图,共享Y轴
ax2 = subplot(1, 2, 2);
plot(ax2, cos(y), y);
xlabel(ax2, 'cos(y)');

% 链接Y轴
linkaxes([ax1, ax2], 'y');

5.3 共享颜色条 #

matlab
figure;

% 创建布局
t = tiledlayout(1, 2);

% 第一个图
nexttile;
[X, Y] = meshgrid(-2:0.2:2);
Z1 = X .* exp(-X.^2 - Y.^2);
surf(X, Y, Z1);
title('曲面1');

% 第二个图
nexttile;
Z2 = -X .* exp(-X.^2 - Y.^2);
surf(X, Y, Z2);
title('曲面2');

% 共享颜色条
cb = colorbar;
cb.Layout.Tile = 'east';

六、实用示例 #

6.1 数据分析仪表板 #

matlab
function create_dashboard(data)
    figure('Name', '数据分析仪表板', 'Position', [100 100 1200 800]);
    
    t = tiledlayout(3, 3, 'TileSpacing', 'compact', 'Padding', 'compact');
    
    % 时序图(跨越3列)
    nexttile([1 3]);
    plot(data.time, data.values);
    title('时序数据');
    xlabel('时间');
    ylabel('数值');
    grid on;
    
    % 直方图
    nexttile;
    histogram(data.values);
    title('数据分布');
    xlabel('数值');
    ylabel('频数');
    
    % 箱线图
    nexttile;
    boxchart(data.values);
    title('箱线图');
    
    % 散点图
    nexttile;
    scatter(data.x, data.y);
    title('相关性');
    xlabel('X');
    ylabel('Y');
    
    % 统计信息
    nexttile([1 3]);
    axis off;
    text(0.1, 0.8, sprintf('均值: %.2f', mean(data.values)), 'FontSize', 12);
    text(0.1, 0.6, sprintf('标准差: %.2f', std(data.values)), 'FontSize', 12);
    text(0.1, 0.4, sprintf('最小值: %.2f', min(data.values)), 'FontSize', 12);
    text(0.1, 0.2, sprintf('最大值: %.2f', max(data.values)), 'FontSize', 12);
    
    % 总标题
    title(t, '数据分析仪表板');
end

6.2 对比图 #

matlab
function comparison_plot(x, y1, y2, labels)
    figure('Name', '对比图', 'Position', [100 100 1000 600]);
    
    t = tiledlayout(2, 2, 'TileSpacing', 'compact');
    
    % 上图:两个数据
    nexttile([1 2]);
    plot(x, y1, 'b-', x, y2, 'r--');
    legend(labels);
    title('对比');
    grid on;
    
    % 下左:数据1
    nexttile;
    plot(x, y1, 'b-');
    title(labels{1});
    grid on;
    
    % 下右:数据2
    nexttile;
    plot(x, y2, 'r--');
    title(labels{2});
    grid on;
end

七、总结 #

本章学习了:

  1. subplot:基本用法、不均匀布局、跨越位置
  2. tiledlayout:基本用法、间距设置、跨越瓦片
  3. axes:自定义位置、嵌套坐标轴
  4. 多窗口:创建多个figure
  5. 共享坐标轴:linkaxes、共享颜色条

下一章将学习图形标注。

最后更新:2026-03-27