0

I have 2 vectors called R_per and A_per with some distinct values. They share a common base called the per.

I need to plot both R_per and A_per w.r.t Per in a Bar plot. The bar must be grouped as shown in this picture:

http://it.mathworks.com/help/releases/R2014b/examples/graphics/BarGraphof2DArrayExample_01.png

They must also have a common X axis scaling.

I tried using plotyy and wrote the following code:

[hyy,hl,ho]=plotyy(per , A_per, per, R_per,'bar','bar');
xt = get(gca, 'XTick');
set(gca, 'XTick', xt, 'XTickLabel', {'200' '300' '400' '500' '700'})
set(ho,'facecolor','r','barwidth',0.2)
set(hl,'facecolor','g','barwidth',0.3)

What i get is a overlapped bar plot with uneven spacing and unclean Y axes.

I am unable to share my plot as i don't not have sufficient reputation Can someone help ?

vittal rao
  • 17
  • 1
  • 8

1 Answers1

0

You can accomplish this using the bar function by combining R_per and A_per into a matrix:

AR_per = [A_per(:), R_per(:)];
bar(per,AR_per);

This will work as long as A_per and R_per and per all have the same length.

Trogdor
  • 1,346
  • 9
  • 16
  • Thank you this works and i have tweaked it to my needs. The only problem being the wide space in the plot as i miss one of the values. Can you help me with this ? – vittal rao Sep 03 '15 at 10:10
  • Unfortunately I don't know of a good way to do that, it would require modifying low-level properties of the figure – Trogdor Sep 08 '15 at 18:53
  • 1
    I managed to fix it by modifying the XTick and string length. Many Thanks !! – vittal rao Sep 10 '15 at 14:19