Bubble Chart of Species Measurements

Using the iris dataset from sci-kit-learn, create a bubble chart showcasing the relationship between petal length (x-axis), sepal length (y-axis), and petal width (size of the bubble). Differentiate each species with a unique color.

 

Example Output:

Use Matplotlib’s scatter method and the s parameter for bubble size.

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

def bubble_chart_species():
    iris = load_iris()
    colors = ['red', 'green', 'blue']
    for target, color in zip(range(3), colors):
        subset = iris.data[iris.target == target]
        plt.scatter(subset[:, 2], subset[:, 0], s=50*subset[:, 3], color=color, label=iris.target_names[target], alpha=0.6, edgecolors='w', linewidth=0.5)
    plt.xlabel("Petal Length (cm)")
    plt.ylabel("Sepal Length (cm)")
    plt.title("Bubble Chart of Petal Length, Sepal Length and Petal Width")
    plt.legend()
    plt.grid(True)
    plt.show()

# Example usage
bubble_chart_species()

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!