drawing bar graph

10 Ansichten (letzte 30 Tage)
Sumit
Sumit am 2 Apr. 2011
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..

Akzeptierte Antwort

the cyclist
the cyclist am 2 Apr. 2011
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 Kommentare
the cyclist
the cyclist am 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 am 18 Jul. 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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jarrod Rivituso
Jarrod Rivituso am 2 Apr. 2011
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 Kommentar
Sumit
Sumit am 3 Apr. 2011
barh would produce horizontal bars which i dont want ...i want vertical bars.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by