How to create a histogram plot with unequal bins?

LS,
I would like to generate a histogram plot for a dataset which is like:
a= [-99999999 -2 -2 -2 -1 -1 0 0 0 1 1 2]
When I use the normal hist function
hist(a)
only two bars are shown; one for the outlying value and one for the values around zero. The details of the values around zero disappear in the graph. Is there a way to avoid this? Can I generate bins from -2 to 2 and a separate bin for everything smaller than -3?
Your help is really appreciated.
Best regards,
Frits Hermans

 採用された回答

Laura Proctor
Laura Proctor 2011 年 5 月 26 日

0 投票

b = -3:2; % specify bin centers
n = hist(a,b) % generate numbers to populate bins
bar(b,n) % plot the resultant histogram

3 件のコメント

Frits
Frits 2011 年 5 月 26 日
Hi Laura,
Thanks a lot for your answer.
Your code works fine except that it does not take into account the large negative value is put in the -2 bin. Is there a possibility to put that value in a separate bin dedicated to values less than -2? The purpose is to count real outliers.
Thanks for your help!
the cyclist
the cyclist 2011 年 5 月 26 日
In your case, I think you probably want to use histc(), not hist(). Then, you can specify that you want your first (left-most) bin edge to be -2.
Then, set the XLim property such that you see only the extent of the plot that you want to.
Walter Roberson
Walter Roberson 2011 年 5 月 26 日
Specify the first bin as negative infinity to catch all of the outliers.

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2011 年 5 月 26 日

0 投票

You can specify the bins precisely, using either the hist() or histc() functions. May I recommend a careful read of the documentation, which spells out how to do this.
>> doc hist
>> doc histc
Specifically, look at the syntax
>> hist(Y,x)
which specifies the locations of the centers of the bins, or
>> histc(Y,edges)
which specifies the locations of the edges.

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by