Borrar filtros
Borrar filtros

drawing bar graph

3 visualizaciones (últimos 30 días)
Sumit
Sumit el 2 de Abr. de 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..

Respuesta aceptada

the cyclist
the cyclist el 2 de Abr. de 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 comentarios
the cyclist
the cyclist el 4 de Abr. de 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 el 18 de Jul. de 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?

Iniciar sesión para comentar.

Más respuestas (1)

Jarrod Rivituso
Jarrod Rivituso el 2 de Abr. de 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 comentario
Sumit
Sumit el 3 de Abr. de 2011
barh would produce horizontal bars which i dont want ...i want vertical bars.

Iniciar sesión para comentar.

Categorías

Más información sobre Discrete Data Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by