4
$\begingroup$

HistogramList gives the possibility to bin the elements in a list. Is it possible to have a logarithmic spacing? In an histogram I would do something like:

Histogram[data, "Log"]

I cannot find anything in the documentation...

$\endgroup$
1
  • $\begingroup$ I have updated my answer. In the documentation, it is under Scope and Bin specifications and Height specifications. $\endgroup$
    – Syed
    Commented Dec 13, 2021 at 5:17

2 Answers 2

5
$\begingroup$

EDIT

HistogramList[data, "Log", "Probability"]

or for the counts:

HistogramList[data, "Log", "Count"]

ListPlot[Last@%]

Original answer

data = RandomVariate[NormalDistribution[0, 1], 200];
Histogram[data]

enter image description here

Histogram[data, ScalingFunctions -> {"Log", "Log"}]

enter image description here

Histogram[data, {"Log", "Sturges"}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks for the answer, but I know how to draw a logarithmic histogram, I want to know how to extract only the height of the bins with logarithmic spacings using HistogramList $\endgroup$
    – mattiav27
    Commented Dec 13, 2021 at 5:08
6
$\begingroup$

As you found in the documentation

HistogramList[data, "Log"]

gives you a logarithmic spacing. But if you're going to create a histogram figure, you'll also need to adjust the heights of the histogram bars so that the area sums to 1 or sums to the sample size depending on the objective. Here's an example:

(* Generate some data *)
data = RandomVariate[LogNormalDistribution[0, 0.5], 1000];

(* Histogram based on the log of the data *)
h = HistogramList[Log[data]];

(* Convert back to original units *)
h[[1]] = Exp[h[[1]]];

(* Generate histogram boxes *)
n = Length[data];
logH=Table[{{h[[1,i]],0},{h[[1,i]],(h[[2,i]]/n)/(h[[1,i+1]]-h[[1,i]])},
{h[[1,i+1]],(h[[2,i]]/n)/(h[[1,i+1]]-h[[1,i]])},{h[[1,i+1]],0}},{i, Length[h[[2]]]}];

(* Plot histogram and true density *)
Show[ListLinePlot[logH, PlotStyle -> {{Thin, Black}}],
 Plot[PDF[LogNormalDistribution[0, 0.5], x], {x, 0, 4}]]

Histogram with logarithmic spacing and true density

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.