MATLAB Answers

double y axis bar chart to have bars next to each other

10 views (last 30 days)
Andrew Alkiviades
Andrew Alkiviades on 7 Sep 2013
hi I have plotted a double y axis bar chart with the code below. It seems that the bars are overlaying. I am trying to get the bars so that they are next to each other on the same x position
Any help would be appreciated!
Wind_monthly_speed = [4.76 3.99 3.43 3.68 5.71 7.09 5.56 5.43 5.4 4.9 3.88 4.53];
Solar_monthly = [5.87 6.52 6.99 6.45 5.6 4.84 5.17 5.66 5.85 5.9 5.29 5.41];
month = 1:1:12;
average_monthly_wind_density = 1.2;
wind_monthly_power = 0.5*average_monthly_wind_density.*(Wind_monthly_speed.^3);
figure;
x1 = 1:12;
y1 = wind_monthly_power;
y2 = Solar_monthly;
plotyy(x1,y1,x1,y2,'bar','bar')

  4 Comments

Show 1 older comment
Andrew Alkiviades
Andrew Alkiviades on 7 Sep 2013
thanks for your reply. I need the 2 axis because there is about a factor of 100 between the 2 vectors and they also use different units so i need the 2 axis
dpb
dpb on 7 Sep 2013
That'll end up scaling them grossly anyway, why not simply multiply the one by 100 and annotate it to note the change?
How about pasting about half your data for somebody to play with?
Andrew Alkiviades
Andrew Alkiviades on 7 Sep 2013
thanks dpb I have updated the initial question above. I will have to use the vectors above, I cannot modify unfortunately

Sign in to comment.

Answers (1)

dpb
dpb on 7 Sep 2013
Edited: dpb on 8 Sep 2013
Well, I think it's much more informative as
bar(x1', [y1' y2'])
but suit yourself. As a starting point try
ha=plotyy(x1-0.5,y1,x1+0.5,y2,'bar','bar');
set(ha,'xlim',[-1 14])
set(ha,'xtick',1:12)
Then get the handles of the bar objects and modify colors and width to enhance visibility, etc., etc., etc., ... as desired
ADDENDUM:
Rather than use +/-0.5, try +/-0.2 save the additional handles from the plotyy() call--
[hyy,hl,ho]=plotyy(x1-0.2,y1,x1+0.2,y2,'bar','bar');
set(hyy,'xlim',[-1 14],'xtick',1:12)
set(ho,'barwidth',0.4)
set(hl,'facecolor','r','barwidth',0.4)

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by