Probability density of log(x) data in matplotlib?

Viewed 53

Displaying a histogram of logarithmically distributed data (in this example, random lognormal noise) is easy to do in matplotlib:

import numpy as np
import matplotlib.pyplot as plt

logx = np.log10(x)
bins = 10**np.linspace(min(logx), max(logx), 101)
plt.hist(x, bins=bins)
plt.xscale('log')

Unnormalised distribution

However, when I try to normalise this distribution by adding density=True to plt.hist, it distorts the distribution due to the different bin widths.

Normalised distribution

How can I make a plot which is a normalised PDF of log(x), where the data distribution and horizontal axis ticks are exactly as they are in the first figure?

1 Answers

This is an artefact of the bin sizes as you approach the smaller end of the scale. Such behaviour will be 'smoothed out' with a larger number of samples:

Low sample size

import numpy as np
import matplotlib.pyplot as plt
from numpy.random import default_rng

rng = default_rng(seed=123)
n = 10000
x = rng.lognormal(0,2,n)

logx = np.log10(x)
bins = 10**np.linspace(min(logx), max(logx), 101)
plt.hist(x, bins=bins, density=True)
plt.xscale('log')

enter image description here

Large sample size

from numpy.random import default_rng

rng = default_rng(seed=123)
n = 10000
x = rng.lognormal(0,2,n*100)

logx = np.log10(x)
bins = 10**np.linspace(min(logx), max(logx), 101)
plt.hist(x, bins=bins, density=True)
plt.xscale('log')

enter image description here

Related