Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get data from Histogram

235 views
Skip to first unread message

Jozef Pulko

unread,
Mar 12, 2009, 3:15:50 AM3/12/09
to
HI to all,

i am using Mathematica 7.0.0.

what I would like to do is to somehow get data out of histogram generated by
Histogram[].

For an example:

I tried to do find the frequency of my data with BinCounts[]... but I need
to manually chose binsize an binwindow.

By using Histogram[] the binsize and binwindow are chosen automatically with
smart algorithms, but only plotted. is there any way to get data out of
plotted histogram?

Thnx,

Jozef


DrMajorBob

unread,
Mar 12, 2009, 6:43:19 AM3/12/09
to
Help has the following example:

{g, {binCounts}} =
Reap[Histogram[
RandomReal[NormalDistribution[0, 1], 100], {-2, 2, 0.25},
Function[{bins, counts}, Sow[counts]]]];

binCounts

{{3, 4, 5, 6, 11, 3, 12, 6, 5, 13, 7, 4, 10, 2, 3, 2}}

Bobby

--
DrMaj...@bigfoot.com

Jozef Pulko

unread,
Mar 13, 2009, 5:48:01 AM3/13/09
to
Thanks for answers, but this didn't solved my problem...

I was trying to get data like it is described in these example...

But this only works if I chose interval and step for histogram. I was not
able to get data out of histogram when letting Mathematica automatically
chose step and interval for building the histogram.

Jozef

Jozef Pulko

unread,
Mar 13, 2009, 5:48:23 AM3/13/09
to
Well that is exactly what I was looking for. Thanks!!

SOLUTION:
h = Histogram[RandomReal[NormalDistribution[0, 1], 200]]
counts = Cases[h, Tooltip[_, Style[label_, __]] :> label, Infinity]

However I only get number of counts in a bin.. is it possible to somehow =
extract also the bin positions?


Bob Hanlon

unread,
Mar 13, 2009, 5:53:26 AM3/13/09
to
h = Histogram[RandomReal[NormalDistribution[0, 1], 200]]

Just look at the FullForm and extract whatever you want. The bin interval and count are

bd = Cases[h, RectangleBox[{bl_, _}, {br_, c_}] :> {{bl, br}, c}, Infinity]

{{{-(5/2), -2}, 5}, {{-2, -(3/2)}, 8}, {{-(3/2), -1}, 13},
{{-1, -(1/2)}, 36}, {{-(1/2), 0}, 37}, {{0, 1/2}, 43},
{{1/2, 1}, 23}, {{1, 3/2}, 21}, {{3/2, 2}, 7}, {{2, 5/2}, 5},
{{5/2, 3}, 2}}

bins = bd[[All, 1]] // Flatten // Union

{-(5/2), -2, -(3/2), -1, -(1/2), 0, 1/2, 1, 3/2, 2, 5/2, 3}

counts = bd[[All, 2]]

{5,8,13,36,37,43,23,21,7,5,2}

counts == Cases[h, Tooltip[_, Style[label_, __]] :> label, Infinity]

True


Bob Hanlon

---- Jozef Pulko <jozef...@googlemail.com> wrote:

=============


Well that is exactly what I was looking for. Thanks!!

SOLUTION:
h = Histogram[RandomReal[NormalDistribution[0, 1], 200]]
counts = Cases[h, Tooltip[_, Style[label_, __]] :> label, Infinity]

However I only get number of counts in a bin.. is it possible to somehow extract also the bin positions?


Brett Champion

unread,
Mar 14, 2009, 6:36:45 AM3/14/09
to
On Mar 13, 2009, at 4:53 AM, Bob Hanlon wrote:

> h = Histogram[RandomReal[NormalDistribution[0, 1], 200]]
>
> Just look at the FullForm and extract whatever you want. The bin
> interval and count are
>

The method used in the documentation, as pointed out by DrMajorBob is
better. It gets the information from a documented behavior of
Histogram (namely, what the third argument can be.)

The internal form of the graphic returned by Histogram is not
documented and is subject to change from version to version. For
example, V7.0.1 has a change from V7.0.0 in how the styles are applied
to Histogram's bars so that the size (in bytes) of the graphic is
smaller (by about 20% for a simple example I just tried.)

Brett Champion
Wolfram Research

Brett Champion

unread,
Mar 14, 2009, 6:36:56 AM3/14/09
to
On Mar 13, 2009, at 4:48 AM, Jozef Pulko wrote:

> Thanks for answers, but this didn't solved my problem...
>
> I was trying to get data like it is described in these example...
>
> But this only works if I chose interval and step for histogram. I
> was not
> able to get data out of histogram when letting Mathematica
> automatically
> chose step and interval for building the histogram.
>

This will use the automatic bin selection, and return the bins and
their counts:

{g, {binCounts}} =
Reap[Histogram[
RandomReal[NormalDistribution[0, 1], 100], Automatic,
Function[{bins, counts}, Sow[{bins, counts}];counts]]];

Brett Champion
Wolfram Research

0 new messages