Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal spacing in bar graph in MATLAB

Tags:

plot

matlab

I have this bar graph in MATLAB, created using the bar command:

enter image description here

I was wondering if there is any way to get rid of empty spaces between 2478 and 2886, and between 4314 and 5130

If I can get the bars to have an equal amount of space in between them that would be perfect.

like image 988
Joonie Avatar asked Jan 31 '18 10:01

Joonie


People also ask

What is the ideal space between bars in a bar chart?

A standard bar chart should have gaps between bars that are slightly narrower than the bars. The exceptions to this are the exception of histograms and clustered bar charts.

Which argument of bar () lets you set the thickness of bar?

If we want to change the thickness of the bars then size argument under geom_bar function of ggplot2 package can be used and it can be set according to our need starting from 1.


1 Answers

As described in the documentation of bar,

bar(x,y) draws the bars at the locations specified by x.

which means that this behavior is intended: Each bar is drawn at the exact position specified by x.

To get equally spaced bars, you can use the categorical function, which converts x to a data type which is intended for discrete categories. That way, you tell MATLAB that x is not a numerical vector where x(i) is the x-coordinate of the i-th element, but rather a simple label for that value.

bar(categorical(x), y)

Bar graph with categorical x-values

like image 51
hbaderts Avatar answered Nov 15 '22 06:11

hbaderts