I would like to plot two bar charts side by side using separate y-axis for each corresponding plot.

66 views (last 30 days)
I am plotting two bar charts on the same figure using the yyaxis right and yyaxis left function, so that they have separate y-axes.
However, I would like the bar charts to appear side by side, whereas currently they are overlapping (see image).
Can anyone suggest what to do in order to get the bar plots to appear side by side instead?
Many thanks for your help.

Accepted Answer

Mike Croucher
Mike Croucher on 26 Jan 2022
Edited: Mike Croucher on 26 Jan 2022
% I want one row of plots with 2 columns
%first plot
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
%second plot
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
Mike Croucher
Mike Croucher on 27 Jan 2022
Sorry...I now notice your need for two different scales. Not enough coffee for me. Something like this was done in another post using plotyy: Bar Plot with 2 Y axes and same X- axis - (mathworks.com)
N=5; % number of bars
y1=rand(N,2); y2=100*rand(N,2); % dummy disparate-magnitude data sets
y1(:,2)=nan;y2(:,1)=nan; % NB: column 2, then column 1 are set NaN
x=[1:N].'; % use the serial index to plot against
hAx=plotyy(x,y1,x,y2,@bar,@bar); % plot, save axes handles
set(hAx(2),'xtick',[]) % turn off labels on RH x-axis; keep only one set
set(hAx(1),'xticklabel',[200:100:500 700]) % tick labels on first...

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!