drawing bar graph

조회 수: 3 (최근 30일)
Sumit
Sumit 2011년 4월 2일
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..

채택된 답변

the cyclist
the cyclist 2011년 4월 2일
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
the cyclist
the cyclist 2011년 4월 4일
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 2012년 7월 18일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jarrod Rivituso
Jarrod Rivituso 2011년 4월 2일
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
Sumit
Sumit 2011년 4월 3일
barh would produce horizontal bars which i dont want ...i want vertical bars.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by