drawing bar graph

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..

 Risposta accettata

the cyclist
the cyclist il 2 Apr 2011

0 voti

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.

6 Commenti

Sumit
Sumit il 3 Apr 2011
this solution is workin perfectly...thank u...bt would like a little more help from u...
along with the previous thing i now want to draw a bar from 15-25...that is i want to merge two bars..
and secondly i want to draw a bar from 12-14 and 16-19..that is two bars completely merging with the bar that was rangin from 10-20
Sumit
Sumit il 3 Apr 2011
one more thing here the width of both the bars are same(10-20 and 40-50 that is 10).how to represent if the width is different...for example 1 from 10-20 and the odr from 35-60...plz do reply...
the cyclist
the cyclist il 3 Apr 2011
If you want bars of different widths, you are going to either (a) use handle graphics (as I mentioned in the last sentence of my answer above), or (b) use the "fill" command as Jarrod described in his answer.
You can get the x-coordinates of the bars using the command
>> get(get(hb,'Children'),'XData')
after you have run my code above. Then you can use the "set" command to set new ones. It might be a little overwhelming the first time you try, but this is a very powerful way of having control over every element of the plots. It is very useful to learn about handle graphics.
There is exhaustive info here:
<http://www.mathworks.com/support/tech-notes/1200/1205.html>
Sumit
Sumit il 4 Apr 2011
Thank you for the help but i hv sorted out a another way of arranging the bar width...but i hv one more prob...which is as follows:
I m drawing a bar graph, "figure1" and after executing certain no of codes i want to draw a new bar graph in a different figure.But in my case i m using "Hold off" which is resulting a new figure which is replacing the old one in the figure "figure1"....how do i draw a separate figure..say "figure2"...keeping the old one "figure1" unchanged..
the cyclist
the cyclist il 4 Apr 2011
You should probably have asked this in a separate question, but what you want is the "figure" command, which will open a new figure. (You might want to read the help file for that command.)
Schamun
Schamun il 18 Lug 2012
i have used the codes stated here, i was wondering how i can change the colors of the bar graphs. i have leftBarCenter = 5; middleBarCenter = 10; rightBarCenter = 15; leftBarCenter1 = 20; middleBarCenter1= 25; rightBarCenter1 = 30; leftBarCenter2 = 35; middleBarCenter2= 40; rightBarCenter2 = 45; how can i change the colors so dat all the left bars will be in one color, all middlebars in another color, and all the right bars in another color?

Accedi per commentare.

Più risposte (1)

Jarrod Rivituso
Jarrod Rivituso il 2 Apr 2011

0 voti

Some thoughts I had...
Have you tried barh?
>> barh([15 45])
That will cause the bars to start from x=0, which may not be what you want based on your description.
You always have the opportunity to get low-level using the fill function. Here is an example:
>> axes;
>> hold on;
>> x = [10 10 20 20];
>> y = [0.8 1.2 1.2 0.8];
>> fill(x,y,'r')
>> x = [40 40 50 50];
>> y = [1.8 2.2 2.2 1.8];
>> fill(x,y,'g')
>> xlim([0 60])
>> ylim([0 3])
Hope this helps!

1 Commento

Sumit
Sumit il 3 Apr 2011
barh would produce horizontal bars which i dont want ...i want vertical bars.

Accedi per commentare.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by