Subplots in MATLAB®
How to make Subplots plots in MATLAB® with Plotly.
Upper and Lower Subplots
Create a figure with two stacked subplots. Plot a sine wave in each one.
subplot(2,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
subplot(2,1,2);
y2 = sin(5*x);
plot(x,y2)
fig2plotly(gcf);
Quadrant of Subplots
Create a figure divided into four subplots. Plot a sine wave in each one and title each subplot.
subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')
subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')
subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')
subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')
fig2plotly(gcf);
Subplots with Different Sizes
Create a figure containing with three subplots. Create two subplots across the upper half of the figure and a third subplot that spans the lower half of the figure. Add titles to each subplot.
subplot(2,2,1);
x = linspace(-3.8,3.8);
y_cos = cos(x);
plot(x,y_cos);
title('Subplot 1: Cosine')
subplot(2,2,2);
y_poly = 1 - x.^2./2 + x.^4./24;
plot(x,y_poly,'g');
title('Subplot 2: Polynomial')
subplot(2,2,[3,4]);
plot(x,y_cos,'b',x,y_poly,'g');
title('Subplot 3 and 4: Both')
fig2plotly(gcf);
Replace Subplot with Empty Axes
Create a figure with four stem plots of random data. Then replace the second subplot with empty axes.
for k = 1:4
data = rand(1,10);
subplot(2,2,k)
stem(data)
end
fig2plotly(gcf);
Subplots at Custom Positions
Create a figure with two subplots that are not aligned with grid positions. Specify a custom position for each subplot.
pos1 = [0.1 0.3 0.3 0.3];
subplot('Position',pos1)
y = magic(4);
plot(y)
title('First Subplot')
pos2 = [0.5 0.15 0.4 0.7];
subplot('Position',pos2)
bar(y)
title('Second Subplot')
fig2plotly(gcf);
Modify Axes Properties After Creation
Create a figure with two subplots. Assign the Axes
objects to the variables ax1
and ax2
. Specify the Axes
objects as inputs to the plotting functions to ensure that the functions plot into a specific subplot.
ax1 = subplot(2,1,1);
Z = peaks;
plot(ax1,Z(1:20,:))
ax2 = subplot(2,1,2);
plot(ax2,Z)
fig2plotly(gcf);
Modify the axes by setting properties of the Axes
objects. Change the font size for the upper subplot and the line width for the lower subplot. Some plotting functions set axes properties. Execute plotting functions before specifying axes properties to avoid overriding existing axes property settings. Use dot notation to set properties.
ax1 = subplot(2,1,1);
Z = peaks;
plot(ax1,Z(1:20,:))
ax2 = subplot(2,1,2);
plot(ax2,Z)
ax1.FontSize = 15;
ax2.LineWidth = 2;
fig2plotly(gcf);
Convert Existing Axes to Subplot
Create a line chart. Then convert the axes so that it is the lower subplot of the figure. The subplot function uses the figure in which the original axes existed.
x = linspace(1,10);
y = sin(x);
plot(x,y)
title('Sine Plot')
fig2plotly(gcf);
x = linspace(1,10);
y = sin(x);
plot(x,y)
title('Sine Plot')
ax = gca;
subplot(2,1,2,ax)
fig2plotly(gcf);
Convert Axes in Separate Figures to Subplots
Combine axes that exist in separate figures in a single figure with subplots.
Create two plots in two different figures. Assign the Axes
objects
to the variables ax1
and ax2
.
Assign the Legend
object to the variable lgd
.
figure
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Line Plot 1')
fig2plotly(gcf);
figure
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Line Plot 1')
ax1 = gca;
figure
y2 = 2*sin(x);
plot(x,y2)
title('Line Plot 2')
lgd = legend('2*Sin(x)');
fig2plotly(gcf);
figure
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Line Plot 1')
ax1 = gca;
figure
y2 = 2*sin(x);
plot(x,y2)
title('Line Plot 2')
lgd = legend('2*Sin(x)');
ax2 = gca;
fig2plotly(gcf);
Create copies of the two Axes
objects using copyobj
. Specify the parents of the copied axes as a new figure. Since legends and colorbars do not get copied with the associated axes, copy the legend with the axes.
figure
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Line Plot 1')
ax1 = gca;
figure
y2 = 2*sin(x);
plot(x,y2)
title('Line Plot 2')
lgd = legend('2*Sin(x)');
ax2 = gca;
fnew = figure;
ax1_copy = copyobj(ax1,fnew);
subplot(2,1,1,ax1_copy)
copies = copyobj([ax2,lgd],fnew);
ax2_copy = copies(1);
subplot(2,1,2,ax2_copy)
fig2plotly(gcf);
Subplot
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);
Multiple Graphs on Separate Axes
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));
Custom Sized Subplot
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));
Multiple Graphs on Same Figure
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));
Stacked Subplots
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));
Stacked Subplots with a Shared X-Axis
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));
Subplots with Shared Axes
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));