MATLAB: Drawing bar graph

bar graphs

i wanna draw a bar graph consisting of 2 bars..one ranging 10-20 on the x axis and the other 40-50 on the x axis….the value of y axis does not matter to me..how do i do it??please help..

Best Answer

There are a couple ways to do this. I think this one may be easiest for you. I wrote it in some detail, with parameters so that you could see what is going on.
leftBarCenter = 15;
rightBarCenter = 45;
desiredWidth = 5;
figure
hb=bar([leftBarCenter rightBarCenter],[1 2]);
set(gca,'XLim',[0 60],'XTick',0:5:60); % Not strictly necessary, but shows result better
% Need to calculate relative width
relativeWidth = 2*desiredWidth/(rightBarCenter-leftBarCenter);
set(hb,'BarWidth',relativeWidth)
If you are very familiar with property handles, then you may find a different way easier. The handles of bars have an XData property that stores their absolute X position. You can edit those, instead.