Customized Box Plots

Using the iris dataset from scikit-learn, create box plots for each measurement (sepal length, sepal width, etc.) by species. Customize the outliers with a different symbol and color.

Example Output:

Use the boxplot function from Matplotlib, and customize the appearance of the outliers with the flierprops parameter. Remember, you need to prepare separate data for each species, and consider using a loop to iterate through species for plotting.

import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

def customized_box_plots():
    iris = load_iris()
    labels = iris.feature_names
    fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(10, 5), sharey=True)
    colors = ['red', 'blue', 'green']

    for target, color, ax in zip(range(3), colors, axes):
        data = [iris.data[iris.target == target][:, feature] for feature in range(4)]
        ax.boxplot(data, flierprops=dict(markerfacecolor=color, marker='D'))
        ax.set_title(iris.target_names[target])
        ax.set_xticklabels(labels, rotation=45, fontsize=8)

    plt.suptitle("Customized Box Plots for Iris Dataset")
    plt.show()

# Example usage
customized_box_plots()

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!