drawing bar graph

6 次查看(过去 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 中查找有关 Discrete Data Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by