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

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

% I want one row of plots with 2 columns
tiledlayout(1,2)
%first plot
nexttile
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y)
%second plot
nexttile
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)

3 Comments

Thanks for your reply.
This is how I mean 'side-by-side':
So for each data point on the x-axis I have two data points on the y-axis.
What I would like is for the two individual data sets to correspond to the right y-axis and left y-axis respectively.
The trick for doing that is also included in the plots above. The one on the right hand side is exactly what you want. Here is the same idea done slightly differently
x=[2000,2001,2002,2003,2004];
y1 = [1,2,3,4,5];
y2 = [1.5,2.5,3.5,4.5,5.5];
% Put the data together so it will plot side by side
data = [y1',y2'];
bar(x,data)
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)

Categories

Community Treasure Hunt

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

Start Hunting!