Borrar filtros
Borrar filtros

How to change the distance between the bars in a bar graph

37 visualizaciones (últimos 30 días)
JVM
JVM el 10 de En. de 2017
Editada: KSSV el 12 de En. de 2017
I have made this bar graph
x = [-3,0,2,4,7,10,12]
y = [1,1,0,1,3,3,2]
bar(x,y)
When I run the script it shows me the bar graph but the distance between the bars are not equal. How do I set them to be equal?
  1 comentario
John Chilleri
John Chilleri el 12 de En. de 2017
In your above code, you are creating bars of height y (height 1 for first bar, 1 for second bar, 0 for third bar, and so on...) with the bars centered around the values in x (first bar centered at -3, second bar centered at 0, etc).
If you want to have equal spacing between bars, then create an x that has equal spacing between elements.
If you need x to be spaced between specific values then look into the linspace command.
Otherwise, simply typing
y = [1,1,0,1,3,3,2]
bar(y)
will produce equally spaced bars.

Iniciar sesión para comentar.

Respuestas (1)

KSSV
KSSV el 12 de En. de 2017
Editada: KSSV el 12 de En. de 2017
% x = [-3,0,2,4,7,10,12]
y = [1,1,0,1,3,3,2] ;
dist = 1 ; % give the distance you want >0
x = 1:dist:(1+(length(y)-1)*dist) ;
bar(x,y)

Categorías

Más información sobre Bar 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