Multiple Axes in MATLAB®
How to make Multiple Axes plots in MATLAB® with Plotly.
Multiple Y-Axes
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
fig = figure;
[ax, h1, h2] = plotyy(x, y1, x, y2, 'plot');
xlabel('Time in \mu sec.');
title('Frequency Response');
set(get(ax(1), 'Ylabel'), 'String', 'Low Frequency');
set(get(ax(2), 'Ylabel'), 'String', 'High Frequency');
fig2plotly(fig);
Bar Plot Overlayed With a Line plot
TBdata = [1990 4889 16.4; 1991 5273 17.4; 1992 5382 17.4; 1993 5173 16.5;
1994 4860 15.4; 1995 4675 14.7; 1996 4313 13.5; 1997 4059 12.5;
1998 3855 11.7; 1999 3608 10.8; 2000 3297 9.7; 2001 3332 9.6;
2002 3169 9.0; 2003 3227 9.0; 2004 2989 8.2; 2005 2903 7.9;
2006 2779 7.4; 2007 2725 7.2];
years = TBdata(:,1);
cases = TBdata(:,2);
rate = TBdata(:,3);
fig = figure;
[ax, h1, h2] = plotyy(years, cases, years, rate, 'bar', 'plot');
set(h1, 'FaceColor', [0.8, 0.8, 0.8]);
set(h2, 'LineWidth', 2);
title('Tuberculosis Cases: 1991-2007');
xlabel('Years');
set(get(ax(1), 'Ylabel'), 'String', 'Cases');
set(get(ax(2), 'Ylabel'), 'String', 'Infection rate in cases per thousand');
fig2plotly(fig);
Multiple Colored Linear and Log Y-Axes
x0 = -2;
xf = 2;
fs = 1000;
x = linspace(x0,xf,fs);
sig1 = abs(sin(x).*exp(x));
sig1log = log(sig1);
fig = figure('Color','w');
[ax, s1h1 s1h2] = plotyy(x,sig1,x,sig1,'plot','semilogy');
sig1col = [0 200 90]/255;
sig1logcol = [210 30 50]/255;
set(s1h1,'Color',sig1col,'LineWidth',5);
set(s1h2,'Color',sig1logcol,'LineWidth',5);
set(ax(1),'YColor',sig1col);
set(ax(2),'YColor',sig1logcol);
xlabel('$x$','Interpreter','latex');
set(get(ax(1),'Ylabel'),'String','$\mbox{y (linear)}$','Interpreter','latex')
set(get(ax(2),'Ylabel'),'String','$\mbox{y (log)}$','Interpreter','latex')
text(-1,5,'$y = |sin(x)e^{x}|$','Interpreter','latex');
leg = legend('$\mbox{y(linear)}$ ','$\mbox{y(log)}$ ', 'Location', 'NorthWest');
set(leg,'Interpreter','latex');
grid on
fig2plotly(fig);
Two Y-Axes
trace1 = struct(...
'x', [1, 2, 3], ...
'y', [40, 50, 60], ...
'name', 'yaxis data', ...
'type', 'scatter');
trace2 = struct(...
'x', [2, 3, 4], ...
'y', [4, 5, 6], ...
'name', 'yaxis2 data', ...
'yaxis', 'y2', ...
'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
'title', 'Double Y Axis Example', ...
'yaxis', struct('title', 'yaxis title'), ...
'yaxis2', struct(...
'title', 'yaxis2 title', ...
'titlefont', struct('color', 'rgb(148, 103, 189)'), ...
'tickfont', struct('color', 'rgb(148, 103, 189)'), ...
'overlaying', 'y', ...
'side', 'right'));
plotly(data, struct('layout', layout));
Multiple Y-Axes
trace1 = struct(...
'x', [1, 2, 3], ...
'y', [4, 5, 6], ...
'name', 'yaxis1 data', ...
'type', 'scatter');
trace2 = struct(...
'x', [2, 3, 4], ...
'y', [40, 50, 60], ...
'name', 'yaxis2 data', ...
'yaxis', 'y2', ...
'type', 'scatter');
trace3 = struct(...
'x', [4, 5, 6], ...
'y', [40000, 50000, 60000], ...
'name', 'yaxis3 data', ...
'yaxis', 'y3', ...
'type', 'scatter');
trace4 = struct(...
'x', [5, 6, 7], ...
'y', [400000, 500000, 600000], ...
'name', 'yaxis4 data', ...
'yaxis', 'y4', ...
'type', 'scatter');
data = {trace1, trace2, trace3, trace4};
layout = struct(...
'title', 'multiple y-axes example', ...
'width', 800, ...
'xaxis', struct('domain', [0.3, 0.7]), ...
'yaxis', struct(...
'title', 'yaxis title', ...
'titlefont', struct('color', '#1f77b4'), ...
'tickfont', struct('color', '#1f77b4')), ...
'yaxis2', struct(...
'title', 'yaxis2 title', ...
'titlefont', struct('color', '#ff7f0e'), ...
'tickfont', struct('color', '#ff7f0e'), ...
'anchor', 'free', ...
'overlaying', 'y', ...
'side', 'left', ...
'position', 0.15), ...
'yaxis3', struct(...
'title', 'yaxis4 title', ...
'titlefont', struct('color', '#d62728'), ...
'tickfont', struct('color', '#d62728'), ...
'anchor', 'x', ...
'overlaying', 'y', ...
'side', 'right'), ...
'yaxis4', struct(...
'title', 'yaxis5 title', ...
'titlefont', struct('color', '#9467bd'), ...
'tickfont', struct('color', '#9467bd'), ...
'anchor', 'free', ...
'overlaying', 'y', ...
'side', 'right', ...
'position', 0.85));
plotly(data, struct('layout', layout));