HistogramList
returns a list of bin boundaries and a list of counts. There is one more boundary than counts, and I'd like to pair them up so I can feed it into ListLinePlot
and get an alternative view of a histogram. Here's some code that will do this:
{bins, counts} = N[HistogramList[RandomVariate[NormalDistribution[5, 2], 100]]]
(* => {{0., 2., 4., 6., 8., 10.}, {13., 19., 37., 28., 3.}} *)
points = Transpose[{Riffle[bins, bins], Flatten[{0, Riffle[counts, counts], 0}]}]
(* => {{0., 0}, {0., 13.}, {2., 13.}, {2., 19.}, {4., 19.}, {4., 37.}, {6., 37.},
{6., 28.}, {8., 28.}, {8., 3.}, {10., 3.}, {10., 0}} *)
Note that it also adds in some zeros to bring the resulting curve down to the axis:
ListLinePlot[points]
Is there a simpler and/or more intuitive way of achieving this behavior?
Thread
may be faster thanTranspose
for longer lists. $\endgroup$ArrayPad[]
for starters:Transpose[{Riffle[bins, bins], ArrayPad[Riffle[counts, counts], 1]}]
$\endgroup$Transpose[{Riffle[bins, bins], Flatten[Partition[counts, 2, 1, {-1, 1}, 0]]}]
$\endgroup$