Show Sidebar Hide Sidebar

Subplots in MATLAB

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

% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% Calculate the data for the plots
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);

% Create the stem plot for the Sampled Signal spanning positions 1 & 3 of a 2x2 grid
fig = figure;
subplot(2, 2, [1 3]);
stem(nT,xn);
xlabel('t');
ylabel('x[n]');
title('Sampled Every T=1e-6 ');

% Create the xy plot for the AM Modulated signal in position 2 of a 2x2 grid
subplot(2, 2, 2);
plot(t, xam);
axis([0 200e-6 -2 2]);
xlabel('t');
ylabel('xam(t) ');
title('AM Modulated Signal');

% Create the xy plot for the reconstructed signal in position 4 of a 2x2 grid
subplot(2, 2, 4);
plot(nT, xn);
xlabel('t');
ylabel('x_zoh(t)');
title('Reconstruction at T=4e-6 ');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-three-subplots');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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]));
response = plotly(data, struct('layout', layout, 'filename', 'simple-subplot', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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]));
response = plotly(data, struct('layout', layout, 'filename', 'custom-size-subplot', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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'));
response = plotly(data, struct('layout', layout, 'filename', 'multiple-subplots', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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'));
response = plotly(data, struct('layout', layout, 'filename', 'shared-axes-subplots', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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]));
response = plotly(data, struct('layout', layout, 'filename', 'stacked-subplots', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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]));
response = plotly(data, struct('layout', layout, 'filename', 'stacked-coupled-subplots', 'fileopt', 'overwrite'));
plot_url = response.url
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.