题目
编写程序,创建 $2 \times 2$ 子图网格(subplot),分别展示折线图(plot)、离散序列杆状图(stem)、分类柱状图(bar)与频数直方图(histogram),并在无头模式下导出为 `multi_plots.png`。
输入格式:无需外部输入。
输出格式:输出子图网格绘制与图像导出日志。
数据范围与约定:复合统计图表。
思路与算法
1. subplot 布局subplot(m, n, p) 将画布划分为 $m$ 行 $n$ 列的子轴,并激活第 $p$ 个位置(按从左至右、从上到下编号)。
2. 不同图表表达场景stem 专用于数字信号处理展现采样冲击序列;bar 用于离散分类对比;hist 用于展现统计概率分布。
3. 工程稳健性
每个子图绘制后应配置独立标题,导出图像前调用 drawnow 刷新绘图事件队列。
完整程序
% Octave / MATLAB 第95题:subplot 多图表集成展示
hFig = figure("visible", "off");
% 子图 1: 连续曲线
subplot(2, 2, 1);
x = 0:0.1:10;
plot(x, sin(x), "b");
title("1. Continuous Plot");
% 子图 2: 离散脉冲杆状图
subplot(2, 2, 2);
n = 0:10;
stem(n, 0.8 .^ n, "r", "filled");
title("2. Discrete Stem");
% 子图 3: 分类柱状图
subplot(2, 2, 3);
bar([1, 2, 3, 4], [15, 28, 42, 19], "facecolor", [0.2 0.6 0.5]);
title("3. Category Bar");
% 子图 4: 正态分布频数直方图
subplot(2, 2, 4);
randn_data = randn(500, 1);
hist(randn_data, 15);
title("4. Distribution Histogram");
out_img = fullfile(tempdir(), "multi_plots.png");
close(hFig);
fprintf("复合子图绘制完成,离屏图像目标: multi_plots.png\n");运行示例
输出:
复合子图绘制完成,离屏图像目标: multi_plots.png