Pie Chart of Species Distribution

Using the iris dataset from scikit-learn, plot a pie chart representing the number of samples from each species. Ensure to provide a legend.

Example 1:

Input: Iris dataset species distribution
Output: Pie chart representing the number of samples from each species with a legend.

Use the load_iris method from scikit-learn to fetch the iris dataset and Matplotlib’s pie method for drawing the pie chart.

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

def pie_chart_species():
    iris = load_iris()
    species_counts = np.bincount(iris.target)
    plt.pie(species_counts, labels=iris.target_names, autopct='%1.1f%%')
    plt.title("Species Distribution in Iris Dataset")
    plt.legend()
    plt.show()

# Example usage
pie_chart_species()

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!