Show Sidebar Hide Sidebar

# Multiple Axes in MATLAB

How to make a graph with multiple axes in MATLAB.

#### Multiple y-axes with the plotyy function

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

% Create some data for the two curves to be plotted
x  = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);

% Create a plot with 2 y axes using the plotyy function
fig = figure;
[ax, h1, h2] = plotyy(x, y1, x, y2, 'plot');

% Add title and x axis label
xlabel('Time in \mu sec.');
title('Frequency Response');

% Use the axis handles to set the labels of the y axes
set(get(ax(1), 'Ylabel'), 'String', 'Low Frequency');
set(get(ax(2), 'Ylabel'), 'String', 'High Frequency');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-two-axes-plot');
plotly_url = response.url;


#### plotyy with a Line Plot and a Bar Chart

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

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

% Create a plot with 2 y axes using the plotyy function
% Cases are represented by a bar chart ; Infection rate is represented by an xy plot
fig = figure;
[ax, h1, h2] = plotyy(years, cases, years, rate, 'bar', 'plot');

% Change the bar colors to light gray
set(h1, 'FaceColor', [0.8, 0.8, 0.8]);

% Chnage the thickness of the line
set(h2, 'LineWidth', 2);

% Add title and x axis label
title('Tuberculosis Cases: 1991-2007');
xlabel('Years');

% Use the axis handles to set the labels of the y axes
set(get(ax(1), 'Ylabel'), 'String', 'Cases');
set(get(ax(2), 'Ylabel'), 'String', 'Infection rate in cases per thousand');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-two-axes-plot-bar');
plotly_url = response.url;


#### plotyy and multiple colored linear and log y-axes

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

%domain
x0 = -2;
xf = 2;
%sampling rate
fs = 1000;
%independent variable x
x = linspace(x0,xf,fs);
sig1 = abs(sin(x).*exp(x));
sig1log = log(sig1);

%create a figure object
fig = figure('Color','w');
%plotyy
[ax, s1h1 s1h2] = plotyy(x,sig1,x,sig1,'plot','semilogy');
%sig1 color
sig1col = [0 200 90]/255;
%sig1log color
sig1logcol = [210 30 50]/255;
%style the plot
set(s1h1,'Color',sig1col,'LineWidth',5);
set(s1h2,'Color',sig1logcol,'LineWidth',5);
set(ax(1),'YColor',sig1col);
set(ax(2),'YColor',sig1logcol);

%x-axis and y-axis labels
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
grid on

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-two-axes-plot-style');
plotly_url = response.url;


#### Two Y-Axes

% 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', [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'));
response = plotly(data, struct('layout', layout, 'filename', 'multiple-axes-double', 'fileopt', 'overwrite'));
plot_url = response.url


#### Multiple Y-Axes

% 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], ...
'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));
response = plotly(data, struct('layout', layout, 'filename', 'multiple-axes-multiple', 'fileopt', 'overwrite'));
plot_url = response.url