Can't seem to figure out why my legend is missing the other two target names.
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn import datasets
iris=datasets.load_iris()
plt.figure(figsize=(8,8))
plt.scatter(iris.data[:, 0], iris.data[:, 2], c=iris.target)
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[2])
plt.title('Iris Dataset')
plt.legend(iris.target_names, loc='lower right')
This might not be the best solution, but you will have to loop through the different target types and call scatter with a specific color and label argument:
import matplotlib.pyplot as plt
from sklearn import datasets
iris=datasets.load_iris()
plt.figure(figsize=(8,8))
for target in set(iris.target):
print target
x = [iris.data[i,0] for i in range(len(iris.target)) if iris.target[i]==target]
y = [iris.data[i,2] for i in range(len(iris.target)) if iris.target[i]==target]
plt.scatter(x, y, color=['red', 'blue', 'green'][target], label=iris.target_names[target])
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[2])
plt.title('Iris Dataset')
plt.legend(iris.target_names, loc='lower right')
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.