In mathematica I have plotted a histogram with the PDF option from a list of values.
How can I extract the heights of the bars in the histogram so that I can compare them with those of another histogram?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityTry examplary
h = Histogram[RandomVariate[NormalDistribution[0, 1], 200]]
The bars might be found inside h
as Rectangle-objects:
bars=Cases[Normal@h , Rectangle[a_, b_, c_] :> {a, b}, Infinity] (*{{{-3., 0}, {-2.5, 2.}}, {{-2.5, 0}, {-2., 2.}}, {{-2., 0}, {-1.5,
10.}}, {{-1.5, 0}, {-1., 11.}}, {{-1., 0}, {-0.5, 31.}}, {{-0.5,
0}, {0., 39.}}, {{0., 0}, {0.5, 34.}}, {{0.5, 0}, {1., 34.}}, {{1.,
0}, {1.5, 24.}}, {{1.5, 0}, {2., 10.}}, {{2., 0}, {2.5,
2.}}, {{3.5, 0}, {4., 1.}}}*)
The heights follow to
Map[#[[2, 2]] &, bars]
(*{2., 2., 10., 11., 31., 39., 34., 34., 24., 10., 2., 1.}*)
Hope it helps!
SeedRandom[1]
data = RandomVariate[NormalDistribution[0, 1], 200];
You can use HistogramList
directly on data
:
HistogramList[data][[2]]
{2, 6, 25, 15, 45, 35, 39, 20, 7, 3, 3}
If you have access only to an histogram output (not data
) , you can extract the Tooltip
labels to get the bar heights:
h = Histogram[data]
Cases[h, Tooltip[_, Style[t_, ___]] :> t, All]
{2, 6, 25, 15, 45, 35, 39, 20, 7, 3, 3}
Note: Default bin specifications for BinCounts
and HistogramList
are different:
So BinCounts
does not give the same output:
{BinCounts[data], HistogramList[data][[2]]}
{{2, 31, 60, 74, 27, 6}, {2, 6, 25, 15, 45, 35, 39, 20, 7, 3, 3}}
BinCounts
$\endgroup$