Subplots in MATLAB®

How to make subplots in MATLAB®. Seven examples of stacked, custom-sized, and gridded subplots.


fm = 20e3;
fc = 100e3;
tstep = 100e-9;
tmax  = 200e-6;
t = 0:tstep:tmax;
xam = (1 + cos(2*pi*fm*t)).*cos(2*pi*fc*t);

T = 1e-6;
N = 200;
nT = 0:T:N*T;
xn = (1 + cos(2*pi*fm*nT)).*cos(2*pi*fc*nT);

fig = figure;
subplot(2, 2, [1 3]);
stem(nT,xn);
xlabel('t');
ylabel('x[n]');
title('Sampled Every T=1e-6 ');

subplot(2, 2, 2);
plot(t, xam);
axis([0 200e-6 -2 2]);
xlabel('t');
ylabel('xam(t) ');
title('AM Modulated Signal');

subplot(2, 2, 4);
plot(nT, xn);
xlabel('t');
ylabel('x_zoh(t)');
title('Reconstruction at T=4e-6 ');

fig2plotly(fig);
trace1 = struct(...
  'x', [1, 2, 3], ...
  'y', [4, 5, 6], ...
  'type', 'scatter');

trace2 = struct(...
  'x', [20, 30, 40], ...
  'y', [50, 60, 70], ...
  'xaxis', 'x2', ...
  'yaxis', 'y2', ...
  'type', 'scatter');

data = {trace1, trace2};

layout = struct(...
    'xaxis', struct('domain', [0, 0.45]), ...
    'yaxis2', struct('anchor', 'x2'), ...
    'xaxis2', struct('domain', [0.55, 1]));

plotly(data, struct('layout', layout));
trace1 = struct(...
  'x', [1, 2, 3], ...
  'y', [4, 5, 6], ...
  'type', 'scatter');

trace2 = struct(...
  'x', [20, 30, 40], ...
  'y', [50, 60, 70], ...
  'xaxis', 'x2', ...
  'yaxis', 'y2', ...
  'type', 'scatter');

data = {trace1, trace2};

layout = struct(...
    'xaxis', struct('domain', [0, 0.7]), ...
    'yaxis2', struct('anchor', 'x2'), ...
    'xaxis2', struct('domain', [0.8, 1]));

plotly(data, struct('layout', layout));
trace1 = struct(...
  'x', [1, 2, 3], ...
  'y', [4, 5, 6], ...
  'type', 'scatter');

trace2 = struct(...
  'x', [20, 30, 40], ...
  'y', [50, 60, 70], ...
  'xaxis', 'x2', ...
  'yaxis', 'y2', ...
  'type', 'scatter');

trace3 = struct(...
  'x', [300, 400, 500], ...
  'y', [600, 700, 800], ...
  'xaxis', 'x3', ...
  'yaxis', 'y3', ...
  'type', 'scatter');

trace4 = struct(...
  'x', [4000, 5000, 6000], ...
  'y', [7000, 8000, 9000], ...
  'xaxis', 'x4', ...
  'yaxis', 'y4', ...
  'type', 'scatter');

data = {trace1, trace2, trace3, trace4};

layout = struct(...
    'xaxis', struct('domain', [0, 0.45]), ...
    'yaxis', struct('domain', [0, 0.45]), ...
    'xaxis4', struct(...
      'domain', [0.55, 1], ...
      'anchor', 'y4'), ...
    'xaxis3', struct(...
      'domain', [0, 0.45], ...
      'anchor', 'y3'), ...
    'xaxis2', struct('domain', [0.55, 1]), ...
    'yaxis2', struct(...
      'domain', [0, 0.45], ...
      'anchor', 'x2'), ...
    'yaxis3', struct('domain', [0.55, 1]), ...
    'yaxis4', struct(...
      'domain', [0.55, 1], ...
      'anchor', 'x4'));

plotly(data, struct('layout', layout));

trace1 = struct(...
  'x', [1, 2, 3], ...
  'y', [2, 3, 4], ...
  'type', 'scatter');

trace2 = struct(...
  'x', [20, 30, 40], ...
  'y', [5, 5, 5], ...
  'xaxis', 'x2', ...
  'yaxis', 'y', ...
  'type', 'scatter');

trace3 = struct(...
  'x', [2, 3, 4], ...
  'y', [600, 700, 800], ...
  'xaxis', 'x', ...
  'yaxis', 'y3', ...
  'type', 'scatter');

trace4 = struct(...
  'x', [4000, 5000, 6000], ...
  'y', [7000, 8000, 9000], ...
  'xaxis', 'x4', ...
  'yaxis', 'y4', ...
  'type', 'scatter');

data = {trace1, trace2, trace3, trace4};

layout = struct(...
    'xaxis', struct('domain', [0, 0.45]), ...
    'yaxis', struct('domain', [0, 0.45]), ...
    'xaxis4', struct(...
      'domain', [0.55, 1], ...
      'anchor', 'y4'), ...
    'xaxis2', struct('domain', [0.55, 1]), ...
    'yaxis3', struct('domain', [0.55, 1]), ...
    'yaxis4', struct(...
      'domain', [0.55, 1], ...
      'anchor', 'x4'));

plotly(data, struct('layout', layout));
trace1 = struct(...
  'x', [0, 1, 2], ...
  'y', [10, 11, 12], ...
  'type', 'scatter');

trace2 = struct(...
  'x', [2, 3, 4], ...
  'y', [100, 110, 120], ...
  'xaxis', 'x2', ...
  'yaxis', 'y2', ...
  'type', 'scatter');

trace3 = struct(...
  'x', [3, 4, 5], ...
  'y', [1000, 1100, 1200], ...
  'xaxis', 'x3', ...
  'yaxis', 'y3', ...
  'type', 'scatter');

data = {trace1, trace2, trace3};

layout = struct(...
    'yaxis', struct('domain', [0, 0.266]), ...
    'legend', struct('traceorder', 'reversed'), ...
    'xaxis3', struct('anchor', 'y3'), ...
    'xaxis2', struct('anchor', 'y2'), ...
    'yaxis2', struct('domain', [0.366, 0.633]), ...
    'yaxis3', struct('domain', [0.733, 1]));

plotly(data, struct('layout', layout));
trace1 = struct(...
  'x', [0, 1, 2], ...
  'y', [10, 11, 12], ...
  'type', 'scatter');

trace2 = struct(...
  'x', [2, 3, 4], ...
  'y', [100, 110, 120], ...
  'yaxis', 'y2', ...
  'type', 'scatter');

trace3 = struct(...
  'x', [3, 4, 5], ...
  'y', [1000, 1100, 1200], ...
  'yaxis', 'y3', ...
  'type', 'scatter');

data = {trace1, trace2, trace3};

layout = struct(...
    'yaxis', struct('domain', [0, 0.33]), ...
    'legend', struct('traceorder', 'reversed'), ...
    'yaxis2', struct('domain', [0.33, 0.66]), ...
    'yaxis3', struct('domain', [0.66, 1]));

plotly(data, struct('layout', layout));