↖️ Show all posts

Plot ECDF Charts Python Data Science

Plot an ECDF Chart:

from sklearn import datasets
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

iris = datasets.load_iris()
data_iris = pd.DataFrame(data= np.c_[iris['data'], iris['target']], columns= iris['feature_names'] + ['target'])

def ecdf(data):
    """Compute ECDF for a one-dimensional array of measurements."""
    # Number of data points: n
    n = len(data)
    # x-data for the ECDF: x
    x = np.sort(data)
    # y-data for the ECDF: y
    y = np.arange(1, n+1) / n
    return x, y

# Compute ECDF for versicolor data: x_vers, y_vers
x_set, y_set = ecdf(data_iris[data_iris.target == 0]["petal length (cm)"])
x_vers, y_vers = ecdf(data_iris[data_iris.target == 1]["petal length (cm)"])
x_virg, y_virg = ecdf(data_iris[data_iris.target == 2]["petal length (cm)"])

# Generate plot
plt.plot(x_set, y_set, marker='.', linestyle='none')
plt.plot(x_vers, y_vers, marker='.', linestyle='none')
plt.plot(x_virg, y_virg, marker='.', linestyle='none')

# Make the margins nice
plt.margins(0.02)

# Label the axes
plt.xlabel('petal length (cm)')
plt.ylabel('ECDF')
plt.show()

plot


⬅️ Read previous Read next ➡️