histogram with a specified x-axis limit and bin position

56 views (last 30 days)
Hello Friends,
I have a matrix of a m x n size.
I want to specify the bin size/width, i.e., something like this: bin = [0, 100] Also, I want to specify xlim starting from 0 , and ending at 100 .
I tried something like this: bin = [0;100]; hist(Data, bin)
but it puts 0 as the bin center on x-axis, and does not even show any data plot at around 100, but what I want is to start my histogram plot from 0 (and 0 not to be its center), and to expend it up to 100 . In other words, left tail of histogram should touch 0 without going to -ve x -axis, and the right tail should touch 100 without going above it.
I also tried:
hist(Data [0, 100]);
xlim([0 100]);
but this cuts the plot from middle and shows the plot from 50 above.
I also tried:
hist(Data);
xlim([0 100]);
but it shows almost nothing.
I also tried:
[f,c] = hist(Data);
figure
bar(c,f,10)
axis([0 100 0 100])
but nothing appears in the plot.
I think the problem is happening because I have a data matrix. If I had a single column vector, it would be different, but I want to keep it like this.

Answers (2)

Star Strider
Star Strider on 3 Aug 2014
If you supply hist with a vector of evenly-spaced bin ranges, it will use those for the centers of the bins.
Example:
X = randi(10, 10, 3);
Ctrs = [0.5:1:9.5];
Xcts = hist(X, Ctrs);
figure(1)
bar(Ctrs, Xcts)
  7 Comments
hello_world
hello_world on 4 Aug 2014
Thank you again for your time. I have to say that it was a good try. I tried it with my matrix and did not work.
As each column of matrix represent a variable, I want to plot them separately instead of putting them in a column vector form like you did above with Xv = X(:);
So if I use matrix form, two problems occur:
(1) Plot shrinks close to ' 0 ' (2) Y axis has different range of y limit. If we plot histogram, y axis ranges up to the number of row(observations).
I think previous solution is close to what I need, but I only need to figure out x limit without affecting y limit.
Star Strider
Star Strider on 4 Aug 2014
I admit that I am out of ideas. I’ve covered everything I can think of.
If the default values of the bar plots do not do what you want them to do, you can alter their properties by changing the various Barseries Properties. This involves getting into handle graphics, but with a bit of experimentation, you can likely get what you want.

Sign in to comment.


Image Analyst
Image Analyst on 4 Aug 2014
Why don't you just use histc() to get the counts. I can't figure out how many bins you want. One time you say you want a single bin, then another time you say you might go for multiple bars. I have no idea anymore what you want, but with histc() you specify where the bin edges are and you can get exactly what you want - whatever that may be.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!