Customized Scatter Plot

Using the iris dataset from scikit-learn, create a scatter plot showcasing petal length against petal width. For each data point, adjust the size of the point based on sepal length and its opacity based on sepal width.

Example Output:

Use the scatter method and utilize the s and alpha parameters.

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

def customized_scatter_plot():
    iris = load_iris()
    sizes = iris.data[:, 0] * 50
    alphas = iris.data[:, 1] / max(iris.data[:, 1])
    
    plt.scatter(iris.data[:, 2], iris.data[:, 3], s=sizes, c='blue', alpha=0.5)
    plt.xlabel("Petal Length (cm)")
    plt.ylabel("Petal Width (cm)")
    plt.title("Customized Scatter Plot for Iris Dataset")
    plt.grid(True)
    plt.show()

# Example usage
customized_scatter_plot()

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!