Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have logarithmic bins in a Python histogram

As far as I know the option Log=True in the histogram function only refers to the y-axis.

P.hist(d,bins=50,log=True,alpha=0.5,color='b',histtype='step') 

I need the bins to be equally spaced in log10. Is there something that can do this?

like image 969
Brian Avatar asked Jul 28 '11 07:07

Brian


People also ask

What is a logarithmic histogram?

Description. Plots a log-histogram, as in for example Feiller, Flenley and Olbricht (1992). The intended use of the log-histogram is to examine the fit of a particular density to a set of data, as an alternative to a histogram with a density curve.

What is logarithmic binning?

Logarithmic binning means something totally different to me: usually in statistical physics, "logarithmic binning" is an algorithm when you want to calculate for example earthquake amplitude distribution (or you can call it a kind of histogram). This distribution yields a power-law fit.

How do you format a histogram in Python?

To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and count the values which fall into each of the intervals. Bins are clearly identified as consecutive, non-overlapping intervals of variables. The matplotlib. pyplot.


2 Answers

use logspace() to create a geometric sequence, and pass it to bins parameter. And set the scale of xaxis to log scale.

import pylab as pl import numpy as np  data = np.random.normal(size=10000) pl.hist(data, bins=np.logspace(np.log10(0.1),np.log10(1.0), 50)) pl.gca().set_xscale("log") pl.show() 

enter image description here

like image 200
HYRY Avatar answered Oct 05 '22 06:10

HYRY


The most direct way is to just compute the log10 of the limits, compute linearly spaced bins, and then convert back by raising to the power of 10, as below:

import pylab as pl import numpy as np  data = np.random.normal(size=10000)  MIN, MAX = .01, 10.0  pl.figure() pl.hist(data, bins = 10 ** np.linspace(np.log10(MIN), np.log10(MAX), 50)) pl.gca().set_xscale("log") pl.show() 

log10 spaced bins

like image 40
Nimar Avatar answered Oct 05 '22 05:10

Nimar