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

174 次查看(过去 30 天)
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.

采纳的回答

Mike Croucher
Mike Croucher 2022-1-26
编辑:Mike Croucher 2022-1-26
% 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 个评论
Mike Croucher
Mike Croucher 2022-1-27
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)
Mike Croucher
Mike Croucher 2022-1-27
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...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Bar Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by