Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to plot gamma distribution with alpha and beta parameters in Python using Matplotlib?
The gamma distribution is a continuous probability distribution with two parameters: alpha (shape) and beta (scale). In Python, we can plot gamma distributions using scipy.stats.gamma.pdf() and Matplotlib to visualize how different parameter values affect the distribution shape.
Steps
Set the figure size and adjust the padding between and around the subplots.
Create x values using numpy and y values using
gamma.pdf()function.Plot x and y data points using
plot()method.Use
legend()method to place the legend elements for the plot.To display the figure, use
show()method.
Basic Gamma Distribution Plot
Here's how to create a basic gamma distribution plot with specific alpha and beta parameters ?
import numpy as np
import scipy.stats as stats
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(0, 10, 100)
y = stats.gamma.pdf(x, a=2, scale=1.5)
plt.plot(x, y, "b-", linewidth=2, label=r'$\alpha=2, \beta=1.5$')
plt.xlabel('x')
plt.ylabel('Probability Density')
plt.title('Gamma Distribution')
plt.legend(loc='upper right')
plt.grid(True, alpha=0.3)
plt.show()
Comparing Multiple Gamma Distributions
To better understand the effect of different parameters, let's plot multiple gamma distributions on the same figure ?
import numpy as np
import scipy.stats as stats
from matplotlib import pyplot as plt
plt.figure(figsize=(10, 6))
x = np.linspace(0, 15, 1000)
# Different alpha (shape) values with same beta (scale)
alphas = [1, 2, 5, 7]
beta = 1.5
for alpha in alphas:
y = stats.gamma.pdf(x, a=alpha, scale=beta)
plt.plot(x, y, linewidth=2, label=fr'$\alpha={alpha}, \beta={beta}$')
plt.xlabel('x')
plt.ylabel('Probability Density')
plt.title('Gamma Distribution with Different Shape Parameters')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xlim(0, 15)
plt.show()
Understanding Parameters
Parameter Effects Visualization
This example shows how changing beta (scale) affects the distribution ?
import numpy as np
import scipy.stats as stats
from matplotlib import pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
x = np.linspace(0, 20, 1000)
# Effect of changing alpha (shape)
betas = [0.5, 1.0, 2.0, 3.0]
alpha = 3
for beta in betas:
y = stats.gamma.pdf(x, a=alpha, scale=beta)
ax1.plot(x, y, linewidth=2, label=fr'$\alpha={alpha}, \beta={beta}$')
ax1.set_xlabel('x')
ax1.set_ylabel('Probability Density')
ax1.set_title('Effect of Scale Parameter (?)')
ax1.legend()
ax1.grid(True, alpha=0.3)
# Effect of changing beta (scale)
alphas = [1, 2, 4, 6]
beta = 1.5
for alpha in alphas:
y = stats.gamma.pdf(x, a=alpha, scale=beta)
ax2.plot(x, y, linewidth=2, label=fr'$\alpha={alpha}, \beta={beta}$')
ax2.set_xlabel('x')
ax2.set_ylabel('Probability Density')
ax2.set_title('Effect of Shape Parameter (?)')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Key Points
Alpha (shape parameter): Controls the shape of the distribution. Higher values make it more bell-shaped.
Beta (scale parameter): Controls the spread. Higher values stretch the distribution horizontally.
scipy.stats.gamma: Uses 'a' for alpha and 'scale' for beta parameters.
pdf() function: Returns probability density values for given x coordinates.
Conclusion
The gamma distribution is useful for modeling positive-valued data with specific shape characteristics. Use scipy.stats.gamma.pdf() with alpha and beta parameters to generate probability density values, and Matplotlib to create informative visualizations of how parameter changes affect the distribution shape.
