MATLAB: Histogram plotting where the x-axis is in multiples of 0.1

histogram

I have a set of data of about 4000 values and it ranges from -0.5 to 0.5. Is there a way to plot a histogram that has the x-axis in multiples of 0.1 or 0.01 like the image.

Best Answer

You have two options:
1. You can use the histogram command. When calling histogram you can specify your bins:
binwidth = 0.1;
histogram(X,-0.5:binwidth :0.5)
2. The histogram command will create bars that are touching one another, which doesn't quite match your image. If you want bars that do not touch one another, you will have to bin and plot your data in two steps using histcounts and bar.
binwidth = 0.1;
[N,edges] = histcounts(X,-0.5:binwidth :0.5);
ctrs = (edges(1:end-1)+edges(2:end))/2;
barwidth = 0.5;
bar(ctrs,N,barwidth);
Related Question