DOC Add legend and axis label in nearest neighbors example
@@ -10,6 +10,7 @@ print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
+import seaborn as sns
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets
@@ -27,7 +28,7 @@ h = .02 # step size in the mesh
# Create color maps
cmap_light = ListedColormap(['orange', 'cyan', 'cornflowerblue'])
-cmap_bold = ListedColormap(['darkorange', 'c', 'darkblue'])
+cmap_bold = ['darkorange', 'c', 'darkblue']
for weights in ['uniform', 'distance']:
# we create an instance of Neighbours Classifier and fit the data.
@@ -44,15 +45,17 @@ for weights in ['uniform', 'distance']:
# Put the result into a color plot
Z = Z.reshape(xx.shape)
- plt.figure()
+ plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, cmap=cmap_light)
# Plot also the training points
- plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
- edgecolor='k', s=20)
+ sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=iris.target_names[y],
+ palette=cmap_bold, alpha=1.0, edgecolor="black")
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("3-Class classification (k = %i, weights = '%s')"
% (n_neighbors, weights))
+ plt.xlabel(iris.feature_names[0])
+ plt.ylabel(iris.feature_names[1])
plt.show()
