How to overlay stacked plots?

How can I overlay stacked plots? I have 1 stacked plot of different sinewaves with 7 panels of data. I have another stacked plot also with 7 panels of data, where a filter has been applied to each sinewave. I would like to overlay these plots, so that the filtered sinewave is on top of the original sinewave. (I know I can filter and plot each sinewave individually, then use a tilechart layout. However, when handling our raw data in the future this will be very time consuming for us.)
% ---Sine wave functions---------------------------------------------------
Fs = 30000; % Sample frequency (Samples/Second)
dt = 1/Fs; % (Seconds/Sample)
t = (0:dt:1); % Time domain (Seconds)
data1 = sin(2*pi*t);
data2 = sin(2*pi*3*t);
data3 = sin(2*pi*300*t);
data4 = sin(2*pi*1000*t);
data5 = sin(2*pi*3000*t);
data6 = sin(2*pi*5000*t);
data7 = sin(2*pi*10000*t);
% ---Create matrix and for orginal sine waves-----------------------------
Matrix = [data1' data2' data3' data4' data5' data6' data7'];
% ---Plot 1: stacked plot of original sine waves---------------------------
figure(1);
Stack = stackedplot(Matrix);
set(Stack, 'DisplayLabels', ...
["F=1Hz" "F=3Hz" "F=300Hz" "F=1000Hz" ...
"F=3000Hz" "F=5000Hz" "F=10,000Hz"]);
for i = 1:7
Stack.AxesProperties(i).YLimits = [-1 1];
end
% ---Filter data-----------------------------------------------------------
% Equiripple Bandpass filter designed using the FIRPM function.
filterFs = 30000; % Sampling frequency for filter
N = 120; % Order
Fstop1 = 0.1; % First Stopband Frequency
Fpass1 = 1; % First Passband Frequency
Fpass2 = 3000; % Second Passband Frequency
Fstop2 = 3300; % Second Stopband Frequency
Wstop1 = 50; % First Stopband Weight
Wpass = 300; % Passband Weight
Wstop2 = 5500; % Second Stopband Weight
dens = 20; % Density Factor
% Calculate the coefficients using the FIRPM function.
b = firpm(N, [0 Fstop1 Fpass1 Fpass2 Fstop2 filterFs/2]/(filterFs/2), ...
[0 0 1 1 0 0], [Wstop1 Wpass Wstop2], {dens});
% Apply filter to data
filtData = filter(b, 1, Matrix);
%---Plot 2: stacked plot of filtered sine waves----------------------------
figure(2);
filtStack = stackedplot(filtData);
set(filtStack, 'DisplayLabels', ...
["F=1Hz" "F=3Hz" "F=300Hz" "F=1000Hz" ...
"F=3000Hz" "F=5000Hz" "F=10,000Hz"]);
for i = 1:7
filtStack.AxesProperties(i).YLimits = [-1 1];
end
%---Plot 3: stacked plot of original and filtered sine waves overlayed-----

Answers (1)

You can use a single stackedplot with multiple table inputs. If the table columns names are the same, then the plots are overlayed:
% ---Sine wave functions---------------------------------------------------
Fs = 30000; % Sample frequency (Samples/Second)
dt = 1/Fs; % (Seconds/Sample)
t = (0:dt:1); % Time domain (Seconds)
data1 = sin(2*pi*t);
data2 = sin(2*pi*3*t);
data3 = sin(2*pi*300*t);
data4 = sin(2*pi*1000*t);
data5 = sin(2*pi*3000*t);
data6 = sin(2*pi*5000*t);
data7 = sin(2*pi*10000*t);
% ---Create matrix and for orginal sine waves-----------------------------
Matrix = [data1' data2' data3' data4' data5' data6' data7'];
% ---Filter data-----------------------------------------------------------
% Equiripple Bandpass filter designed using the FIRPM function.
filterFs = 30000; % Sampling frequency for filter
N = 120; % Order
Fstop1 = 0.1; % First Stopband Frequency
Fpass1 = 1; % First Passband Frequency
Fpass2 = 3000; % Second Passband Frequency
Fstop2 = 3300; % Second Stopband Frequency
Wstop1 = 50; % First Stopband Weight
Wpass = 300; % Passband Weight
Wstop2 = 5500; % Second Stopband Weight
dens = 20; % Density Factor
% Calculate the coefficients using the FIRPM function.
b = firpm(N, [0 Fstop1 Fpass1 Fpass2 Fstop2 filterFs/2]/(filterFs/2), ...
[0 0 1 1 0 0], [Wstop1 Wpass Wstop2], {dens});
% Apply filter to data
filtData = filter(b, 1, Matrix);
%--- stacked plot of filtered sine waves----------------------------
figure(1);
T1 = array2table(Matrix, VariableNames={'X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7'});
T2 = array2table(filtData, VariableNames={'X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7'});
filtStack = stackedplot(T1, T2);
set(filtStack, 'DisplayLabels', ...
["F=1Hz" "F=3Hz" "F=300Hz" "F=1000Hz" ...
"F=3000Hz" "F=5000Hz" "F=10,000Hz"]);
for i = 1:7
filtStack.AxesProperties(i).YLimits = [-1 1];
end

2 Comments

Thank you so much!!
This cannot be done with a matrix, correct? Only tables or timetables?

Sign in to comment.

Asked:

on 4 Feb 2026

Commented:

on 4 Feb 2026

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!