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

228 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Mike Croucher
Mike Croucher le 26 Jan 2022
Modifié(e) : Mike Croucher le 26 Jan 2022
% 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 commentaires
Mike Croucher
Mike Croucher le 27 Jan 2022
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 le 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...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by