Using the iris dataset from scikit-learn, plot a line chart representing the average sepal length, sepal width, petal length, and petal width of each species across the dataset. Ensure each series is differentiated with unique colors and provide a legend.
Example Output:
Iterate over each measurement (sepal length, sepal width, etc.) and plot them on the same axis. Use Matplotlib’s plot
method for drawing the line plot and legend
for the legend.
import matplotlib.pyplot as plt from sklearn.datasets import load_iris import numpy as np def multi_series_line_plot(): iris = load_iris() measurements = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"] colors = ['red', 'green', 'blue', 'purple'] species = iris.target_names for i, color in enumerate(colors): avg_values = [np.mean(iris.data[iris.target == j][:, i]) for j in range(3)] plt.plot(species, avg_values, color=color, label=measurements[i]) plt.xlabel("Species") plt.ylabel("Average Measurement (cm)") plt.title("Average Measurements by Species") plt.legend() plt.grid(True) plt.show() # Example usage multi_series_line_plot()
Unlock AI & Data Science treasures. Log in!