matlabcontrol-charts# Plot control at one figure and two subplots using built-in matlab functions

*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;
```

Solution

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';
```

- how to reshape a cell array to matrix in desired size
- Using Mat_CreateVer for MATLAB versions higher than 7.3
- Making a numpy ndarray matrix symmetric
- Matlab 2021 and2023 cross compatability
- How to plot figures at the end of the for loop MATLAB?
- Variable transfer from App Designer to general .m script file
- Anomalous behavior of the Simulink signal - Simscape
- What is the fastest method to searsch for a part of a matrix inside of a large matrix?
- Can I Prevent Git from Using XTerm Escape Sequences?
- Use VPA to evaluate tiny probabilities
- Generate Array of Random Numbers with Specified Standard Deviation and Mean
- Matlab update plot with multiple data lines/curves
- Matlab vectorize for loop (grid depends on loop index)
- How to use a flag variable to break all the for-loops in spmd statement?
- How to produce the indexes from pdist2 function in Octave?
- Calculation of sensitivity and specificity from confusion matrix
- How do I read comma separated values from a .txt file in MATLAB using textscan()?
- How do you code Garson's algorithm in matlab to find the relative importance of parameters when training a neural network model?
- add data label to a grouped bar chart in matlab
- Using 1D convolutional with same shape so it will work with FFT?
- super fast median of matrix in OpenCV (as fast as MATLAB)
- How to create a struct or type from a dictionary in Julia?
- MATLAB Delay an Input Signal
- Is there an Octave equivalent of Matlab's `contains` function?
- Matlab define callback function for mouse click on a biograph
- getting a return value from a function in MATLAB
- How to convert nifti file to AFNI file?
- load .mat file, but not on the workspace
- Fast plane fitting to many points
- Cubic spline interpolation vs polynomial interpolation