You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab中如何在单图双子图绘制带网格的controlchart控制图?

解决MATLAB中xbar-R控制图同画布绘制及网格显示问题

问题说明

使用MATLAB的controlchart()函数绘制xbar控制图和R控制图时,遇到两个问题:

  • 无法将两个图合并到同一画布的子图中,总是生成独立窗口
  • 网格仅能显示在最后一个绘制的图上,无法同时作用于两个图

尝试过以下两种方法均失败:

  1. 先创建子图再分别调用controlchart()
  2. 直接指定charttype为{'xbar','r'}后添加网格

解决方案

核心问题是controlchart()默认会创建新的Figure窗口,忽略当前的子图上下文。解决思路是通过'Parent'参数手动指定绘图的子图容器,再分别为每个子图设置网格和标题。

完整可运行代码

%% 参数设置
mu = 101;
sigma = 1;
n = 100;
sample_size = 5;

%% 生成模拟数据
initial_points = randn(25 + randi(6, 1), sample_size) * sigma + mu;

flag = randi(3, 1);
if flag == 1
    mu_new = mu + randi([-1, 1]) * randi([1, 2]) * 0.15;
    after_points = randn(74 + randi(6, 1), sample_size) * sigma + mu_new;
elseif flag == 2
    sigma_new = sigma + randi([2, 3]) * 0.05;
    after_points = randn(74 + randi(6, 1), sample_size) * sigma_new + mu;
else
    after_points = randn(74 + randi(6, 1), sample_size) * sigma + mu;
end
data  = [initial_points; after_points];

%% 同画布绘制双控制图
figure('Position',[100 100 800 600]) % 设置画布大小

% 创建子图并保存句柄
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);

% 绘制xbar图,指定父容器为ax1
controlchart(data, 'charttype', 'xbar', 'Parent', ax1);
grid(ax1, 'on');
grid(ax1, 'minor');
title(ax1, 'Xbar 控制图');

% 绘制R图,指定父容器为ax2
controlchart(data, 'charttype', 'r', 'Parent', ax2);
grid(ax2, 'on');
grid(ax2, 'minor');
title(ax2, 'R 控制图');

额外说明:一次性绘制双图后添加网格

如果想用controlchart(data, 'charttype', {'xbar','r'})的方式一次性生成双图,需要遍历所有轴对象设置网格:

st = controlchart(data, 'charttype', {'xbar','r'});
% 获取所有轴句柄
axes_handles = findall(gcf, 'Type', 'Axes');
% 遍历开启网格
for ax = axes_handles
    grid(ax, 'on');
    grid(ax, 'minor');
end

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 14:03:18