Reputation: 329
I try to plot control chart plot, and add to grid to every plot. But if I try to plot charts: for examples xbar
and r
charts, using controlchart()
I get only last second plot (r
chart), with grid.
My trouble: how to plot two plots and one figure non separatly, for example, two subplots?
My code for plotting control charts:
Example Data: (for every rows calculate mean and r data for CCharts plot)
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;
...
Code:
%% 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');
And I want to plot two CCharts with grids, and code not work:
% 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;
This code also not work:
st1 = controlchart(data, 'charttype', {'xbar','r'});
grid on;
grid minor;
Upvotes: 2
Views: 89
Reputation: 141
The parent
parameter can be used to send the plots to subplot axes. From the controlchart
documentation:
parent — The handle of the axes to receive the control chart plot. Default is to create axes in a new figure. Not permitted if there are multiple chart types.
% 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;
Since the legends are the same, you could use tiledlayout
to place a single legend for the figure.
%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';
Upvotes: 2