使用内置的matlab函数在一个图和两个子图上进行绘图控制

dojqjjoe  于 6个月前  发布在  Matlab
关注(0)|答案(1)|浏览(91)
  • 我尝试绘制控制图图 并将 * 网格添加到每个图 *。但是如果我尝试绘制图表:例如xbarr图表,使用controlchart()我只能得到最后一秒的图(r图表),带有网格。
    我的麻烦: 如何绘制两个情节和一个数字不分开,例如,两个子情节?
  • 绘制控制图的代码:*
    示例数据:(* 为每一行计算CCarts图的平均值和r数据 *)
102.8339   99.8529  101.0326  101.1049  101.8252 ;

   98.7412   99.9311  101.5525  101.7223  102.3790;

  101.8622  100.1905  102.1006  103.5855   99.9418;

  101.3188   98.0557  102.5442  100.3331  100.5314;

   99.6923  102.4384  101.0859  101.1873  100.7275;

...

字符串

产品代码:

%% Parameters for the initial normal distribution
mu = 101;
sigma = 1;

%% Number of data points
n = 100; % Total number of data points
%% Sample size for each point on the control chart
sample_size = 5;

%% Generate the first 25-30 points with a normal distribution
initial_points = randn(25 + randi(6, 1), sample_size) * sigma + mu;

%% Generate the control chart data
        flag = randi(3, 1);
        if flag == 1
            mu_new = mu + randi([-1, 1]) * randi([1, 2]) * 0.15; % Randomly update mu
            after_points = randn(74 + randi(6, 1), sample_size) * sigma + mu_new;
        elseif flag == 2
            sigma_new = sigma + randi([2, 3]) * 0.05; % Randomly update sigma
            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];

%% Create figure and plot
figure(1)

% Subplot 1 for the first control chart ('u' chart)
st1 = controlchart(data, 'charttype', 'xbar');
grid on;
grid minor;
title('Control Chart - Type xbar');

figure(2)
% Subplot 2 for the second control chart ('p' chart)
st2 = controlchart(data, 'charttype', {'r'});
grid on;
grid minor;
title('Control Chart - Type r');


我想用网格绘制两个CCarts,代码不工作:

% Create figure
figure;

% Subplot 1 for the first control chart ('xbar' chart)
subplot(2, 1, 1);
st1 = controlchart(data, 'charttype', {'xbar'});
grid on;
grid minor;

% Subplot 2 for the second control chart ('r' chart)
subplot(2, 1, 2);
st2 = controlchart(data, 'charttype', {'r'});
grid on;
grid minor;


这段代码也不行:

st1 = controlchart(data, 'charttype', {'xbar','r'});
grid on;
grid minor;

lb3vh1jj

lb3vh1jj1#

parent参数可用于将图发送到子图轴。在controlchart文档中:
父项-接收控制图绘图的坐标轴控点。预设值是在新图形中建立坐标轴。如果有多种图表类型,则不允许使用。

% Create figure
figure;

% Subplot 1 for the first control chart ('xbar' chart)
subplot(2, 1, 1);
st1 = controlchart(data, 'charttype', {'xbar'},'parent',gca);
grid on;
grid minor;

% Subplot 2 for the second control chart ('r' chart)
subplot(2, 1, 2);
st2 = controlchart(data, 'charttype', {'r'},'parent',gca);
grid on;
grid minor;

字符串
2x1 subplot
由于图例是相同的,因此您可以使用tiledlayout为图形放置单一图例。

%Create figure
tl = tiledlayout(2,1);

% Subplot 1 for the first control chart ('xbar' chart)
ax1 = nexttile;
st1 = controlchart(data, 'charttype', {'xbar'},'parent',ax1);
grid on;
grid minor;

% Subplot 2 for the second control chart ('r' chart)
ax2 = nexttile;
st2 = controlchart(data, 'charttype', {'r'},'parent',ax2);
grid on;
grid minor;

% Make single legend at top
delete(ax1.Legend)
ax2.Legend.Orientation = 'horizontal';
ax2.Legend.Layout.Tile = 'north';


2x1 tiledlayout

相关问题