how to output the data set when using histogram in mathematica
In Mma, the Histogram function only generates graphics. I am wonderi开发者_开发知识库ng how I can to get the data set; is there any convenient built-in function for this?
Many thanks.
HistogramList was added to Mathematica V8 to expose the binning and height calculations.
HistogramList[a]
For V7 you can hack the third argument to get the bins and counts.
Histogram[a, Automatic, (Print[{##}]; #2) &]
Perhaps a bit of internet searching would help as well. This was my answer (of June 18, 2010) to a similar question in the Mathematica newsgroup comp.soft-sys.math.mathematica:
data = RandomReal[NormalDistribution[0, 1], 200];
res = Reap[Histogram[data, Automatic, (Sow[{#1, #2}]; #2) &]]
I feel this solution is slightly better than Brett's because it returns the data in a readily usable format.
EDIT
To recreate the histogram following any bin or count manipulation you'd proceed as suggested by Brett below. Just have the bin and count ready as follows:
bins = Union[ Flatten[res[[2, 1, 1, 1]]]];
counts = res[[2, 1, 1, 2]];
Histogram[data, {bins}, counts &]
I'm not sure whether the bins are guaranteed to be in ascending order, so instead of Union
(which sorts), you might want to use DeleteDuplicates
.
The count &
is a trick here. As explained in the help page, a function in the third position is expected to take a bin and count list and returns a height list. In this case it just gobbles up the lists and returns counts.
If you're using V7 and you're upset that by using this trick you can't use a built-in bin height specification ("Count", "Probability", "ProbabilityDensity", etc), you can modify Sjord's answer above to return bins normalized however you want. For example, if you want bin heights using
data = RandomReal[NormalDistribution[0, 1], 200];
Histogram[data, Automatic, "Probability"]
you could instead use
res = Reap[Histogram[data, Automatic,
(ret = #2/Length[data];
Sow[{#1, ret}]; ret) &
]]
The analog for "ProbabilityDensity" is
res = Reap[Histogram[data, Automatic,
(binWidth = #1[[1]][[2]] - #1[[1]][[1]];
ret = #2/(Length[data]*binWidth);
Sow[{#1, ret}]; ret) &
]]
精彩评论