Using the iris dataset from scikit-learn, create a radar/spider chart showcasing the average measurements (sepal length, sepal width, petal length, and petal width) of each species. Ensure each species has a distinct line style and color.
Example Output:
Use the polar projection in Matplotlib to create a radar chart. Compute the mean for each measurement of each species. To close the radar plot, append the first measurement value at the end. Use different line styles and colors to represent each species. The plot
function can be used to plot data on a polar coordinate system
import matplotlib.pyplot as plt from sklearn.datasets import load_iris import numpy as np def radar_chart(): iris = load_iris() labels = iris.feature_names angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False).tolist() angles += angles[:1] fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) for target, color, linestyle in zip(range(3), ['red', 'blue', 'green'], ['-', '--', '-.']): data = np.mean(iris.data[iris.target == target], axis=0).tolist() data += data[:1] ax.plot(angles, data, color=color, linestyle=linestyle, label=iris.target_names[target]) ax.set_yticklabels([]) ax.set_xticks(angles[:-1]) ax.set_xticklabels(labels) ax.legend() plt.title("Radar Chart of Average Measurements by Species") plt.show() # Example usage radar_chart()
Unlock AI & Data Science treasures. Log in!