15.1 C
London
Friday, July 5, 2024
HomePythonData Visualizations in PythonHow to Calculate & Plot a CDF in Python

How to Calculate & Plot a CDF in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to calculate the cumulative distribution function (CDF) in Python:

#sort data
x = np.sort(data)

#calculate CDF values
y = 1. * np.arange(len(data)) / (len(data) - 1)

#plot CDF
plt.plot(x, y)

The following examples show how to use this syntax in practice.

Example 1: CDF of Random Distribution

The following code shows how to calculate and plot a cumulative distribution function (CDF) for a random sample of data in Python:

import numpy as np
import matplotlib.pyplot as plt

#define random sample of data
data = np.random.randn(10000)

#sort data
x = np.sort(data)

#calculate CDF values
y = 1. * np.arange(len(data)) / (len(data) - 1)

#plot CDF
plt.plot(x, y)
plt.xlabel('x')

The x-axis displays the raw data values and the y-axis displays the corresponding CDF values.

Example 2: CDF of Normal Distribution

If you’d like to plot the cumulative distribution function of a known distribution (such as the normal distribution) then you can use the following functions from the SciPy library:

import numpy as np
import scipy
import matplotlib.pyplot as plt

#generate data from normal distribution
data = np.random.randn(1000)

#sort data
x = np.sort(data)

#calculate CDF values
y = scipy.stats.norm.cdf(x)

#plot CDF
plt.plot(data_sorted, norm_cdf)

#plot CDF
plt.plot(x, y)
plt.xlabel('x')

Additional Resources

CDF vs. PDF: What’s the Difference?
How to Make a Bell Curve in Python
How to Calculate Z-Scores in Python

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories