Table of Contents
The Log-Normal Distribution is a commonly used probability distribution in statistics and mathematics. It is often used to model data that is skewed to the right, meaning that the majority of the data is clustered towards lower values with a few extreme values. In Python, the Log-Normal Distribution can be utilized through the scipy.stats library, which provides functions for calculating the probability density function, cumulative distribution function, and other statistical parameters such as mean and standard deviation. This allows users to easily generate random data that follows a Log-Normal Distribution, as well as perform statistical analyses and visualizations on the data. The Log-Normal Distribution in Python is a useful tool for analyzing real-world data and making informed decisions based on its properties and characteristics.
Use the Log-Normal Distribution in Python
You can use the function from the SciPy library in Python to generate a random variable that follows a log-normal distribution.
The following examples show how to use this function in practice.
How to Generate a Log-Normal Distribution
You can use the following code to generate a random variable that follows a log-normal distribution with μ = 1 and σ = 1:
import math
import numpy as np
from scipy.stats import lognorm
#make this example reproducible
np.random.seed(1)
#generate log-normal distributed random variable with 1000 values
lognorm_values = lognorm.rvs(s=1, scale=math.exp(1), size=1000)
#view first five values
lognorm_values[:5]
array([13.79554017, 1.47438888, 1.60292205, 0.92963 , 6.45856805])
Note that within the lognorm.rvs() function, s is the standard deviation and the value inside math.exp() is the mean for the log-normal distribution that you’d like to generate.
In this example, we defined the mean to be 1 and the standard deviation to also be 1.
How to Plot a Log-Normal Distribution
We can use the following code to create a histogram of the values for the log-normally distributed random variable we created in the previous example:
import matplotlib.pyplotas plt #create histogram plt.hist(lognorm_values, density=True, edgecolor='black')

Matplotlib uses 10 bins in histograms by default, but we can easily increase this number using the bins argument.
For example, we can increase the number of bins to 20:
import matplotlib.pyplotas plt #create histogram plt.hist(lognorm_values, density=True, edgecolor='black', bins=20)

The greater the number of bins, the more narrow the bars will be in the histogram.
Related:
Additional Resources
The following tutorials explain how to work with other probability distributions in Python:
Cite this article
stats writer (2024). How can the Log-Normal Distribution be used in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-log-normal-distribution-be-used-in-python/
stats writer. "How can the Log-Normal Distribution be used in Python?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-log-normal-distribution-be-used-in-python/.
stats writer. "How can the Log-Normal Distribution be used in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-log-normal-distribution-be-used-in-python/.
stats writer (2024) 'How can the Log-Normal Distribution be used in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-log-normal-distribution-be-used-in-python/.
[1] stats writer, "How can the Log-Normal Distribution be used in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the Log-Normal Distribution be used in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
