histogram with a specified x-axis limit and bin position

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)

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

Thank you for your answer, but it is not working for me because:
(1) I want only one bin starting from 0 and ending at 100 . In your case you are creating many centers and bins around them. (2) I do not want my plot to expand below 0 or above 100 .
Please let me know if there is other way.
How about
% Fill first bin with counts of # elements with values 0-100
counts(1) = sum(sum(Data<=100));
% Fill second bin with counts of # elements with values >=100
counts(2) = numel(Data)-counts(1);
If you simply want a single bar summing up all your data, just use the patch function:
X = randi(10, 10, 3);
Xv = X(:);
figure(2)
patch([0 100 100 0],[0 0 [1 1]*length(Xv)], 'b')
Are we getting closer to what you want?
The total number of elements in a histogram are the total number of elements in your data matrix. (I created mine in my X matrix, so here there are 30.) I created a vector out of X in Xv and then got their length (although I could have used the numel function for that), then used that as the Y-value in the patch arguments.
Thank you again, but patch does not give the same information as histogram. My data matrix column represent variables, and I want to plot them as histogram such that xmin has the lowesest value ' 0 ' while xmax has the highest value ' 100 '. Suppose it is not possible to get one single bar, can we have multiple bars within this xmin, xmax range without plot to get out of the limits?
The stairs function along with hist and the area plot may do what you want:
X = randi(101, 1000, 3)-1;
Xv = X(:);
Ctrs = 0:100;
Xcts = hist(Xv, Ctrs);
[xb, yb] = stairs(Ctrs,Xcts);
figure(3)
area(xb, yb)
With my test data, they produce this plot:
Does this do what you want?
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.
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.

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

Asked:

on 3 Aug 2014

Commented:

on 4 Aug 2014

Community Treasure Hunt

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

Start Hunting!