The Gamma Distribution is a continuous probability distribution widely used in various fields such as statistics, engineering, and finance. It is commonly employed to model waiting times, service demands, and lifetime distributions. In this comprehensive guide, we will demonstrate how to plot a distribution in Python using a step-by-step approach.
Understanding the Gamma Distribution Parameters
Before we start plotting, it is essential to understand the parameters that characterize the Gamma distribution. The Gamma distribution has two parameters:
- Shape parameter (k): This parameter, also known as the order or alpha, influences the distribution’s overall shape. It must be a positive number.
- Scale parameter (θ): This parameter, also known as beta, affects the spread of the distribution. It must also be a positive number.
The probability density function (PDF) of the Gamma distribution is given by:
f(x; k, θ) = (x^(k-1) * exp(-x/θ)) / (θ^k * Γ(k))
where x > 0, k > 0, θ > 0, and Γ(k) is the gamma function.
Example 1: Plot One Gamma Distribution
Now that we have a grasp on the Gamma distribution parameters and required libraries, let’s dive into the actual plotting process. We will create a Python function that takes the shape and scale parameters as input and generates a plot of the Gamma distribution.
Here’s an example:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gamma
# Define the parameters of the gamma distribution
shape = 2.5
scale = 1.0
# Create a distribution object
dist = gamma(a=shape, scale=scale)
# Generate a sequence of x-values
x = np.linspace(0, 10, 1000)
# Calculate the corresponding y-values of the gamma distribution
y = dist.pdf(x)
# Plot the distribution
plt.plot(x, y, label='Gamma Distribution')
# Add a legend and axis labels
plt.legend()
plt.xlabel('x')
plt.ylabel('PDF')
# Show the plot
plt.show()
Output:

In this example, we first import the necessary libraries numpy
, matplotlib
, and scipy.stats
. We then define the shape and scale parameters of the gamma distribution, and create a gamma
object using these parameters.
Next, we generate a sequence of x-values over the range of 0 to 10, and calculate the corresponding y-values of the gamma distribution using the pdf()
method of the gamma
object.
Finally, we plot the distribution using matplotlib
, and add a legend and axis labels for clarity. The resulting plot should show a bell-shaped curve with a skew to the right, depending on the chosen shape and scale parameters.
Example 2: Plot Multiple Gamma Distributions
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gamma
# Define the parameters of the distributions
shapes = [1, 2, 3]
scales = [1, 1.5, 2]
# Generate a sequence of x-values
x = np.linspace(0, 10, 1000)
# Plot each distribution
for shape, scale in zip(shapes, scales):
# Create a gamma distribution object
dist = gamma(a=shape, scale=scale)
# Calculate the corresponding y-values of the gamma distribution
y = dist.pdf(x)
# Plot the gamma distribution
plt.plot(x, y, label=f'Gamma({shape}, {scale})')
# Add a legend and axis labels
plt.legend()
plt.xlabel('x')
plt.ylabel('PDF')
# Show the plot
plt.show()
Output:

In this example, we first define lists of shape and scale parameters for the distributions we want to plot. We then generate a sequence of x-values over the range of 0 to 10.
Next, we use a for loop to iterate through each pair of shape and scale parameters, creating a gamma
object with those parameters, calculating the corresponding y-values of the distribution, and plotting the distribution using matplotlib
. We add a label to each plot to indicate the corresponding shape and scale parameters.
Finally, we add a legend and axis labels for clarity, and show the plot. The resulting plot should show three bell-shaped curves, each with a different skew and peak location, corresponding to the chosen shape and scale parameters.
Wrap up
Using Python libraries such as scipy.stats
and matplotlib
, it is easy to plot distributions. By defining the shape and scale parameters of the distribution, generating a sequence of x-values, and plotting the distribution, we can create informative and visually appealing plots. When plotting multiple distributions, we can use lists of shape and scale parameters and a for loop to plot each distribution individually.
To learn more check out the:
https://en.wikipedia.org/wiki/Gamma_distribution
Thanks for reading. Happy coding!