Issue
I am trying to plot the decision boundary of an SVM classifier using iris dataset. The class label doesn't appear on the legend although I set label=y
.
Code:
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.inspection import DecisionBoundaryDisplay
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target
linear = svm.LinearSVC()
linear.fit(X,y)
X0, X1 = X[:, 0], X[:, 1]
fig, ax = plt.subplots(figsize=(10, 6))
disp = DecisionBoundaryDisplay.from_estimator(linear, X,
response_method='predict',cmap=plt.cm.coolwarm, alpha=.8,ax=ax,
xlabel=iris.feature_names[0],ylabel=iris.feature_names[1],label=y)
ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
ax.set_xticks(())
ax.set_yticks(())
ax.set_title('Some title')
ax.legend()
plt.show()
Solution
U could use automatic legend creation. In your case:
scatter = ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k',
)
legend1 = ax.legend(*scatter.legend_elements(),
loc="lower left", title="Classes")
ax.add_artist(legend1)
Answered By - arilwan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.