Basic Histogram Plot

Given the iris dataset from scikit-learn, create a histogram showcasing the distribution of petal lengths. Ensure you title your plot and label your axes appropriately.

Example 1:

Input: Iris dataset petal lengths
Output: Histogram showcasing the distribution of petal lengths.

Example 2:

Input: Iris dataset petal widths
Output: Histogram showcasing the distribution of petal widths.

Use the load_iris method from scikit-learn to fetch the iris dataset and Matplotlib’s hist method for the histogram.

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

def histogram_plot(data, label):
    plt.hist(data)
    plt.xlabel(label)
    plt.ylabel("Frequency")
    plt.title(f"Distribution of {label}")
    plt.grid(True)
    plt.show()

iris = load_iris()
# Example usages
histogram_plot(iris.data[:, 2], "Petal Length (cm)")  # petal length
histogram_plot(iris.data[:, 3], "Petal Width (cm)")  # petal width

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!