繪制二進制分類的散點分布['cat', 'dog']:
X, y = make_classification(n_samples=1000, n_features=2, n_redundant=0,
n_clusters_per_class=1, weights=[0.9], flip_y=0, random_state=1,)
counter = Counter(y)
for label, _ in counter.items():
row_ix = np.where(y == label)[0]
plt.scatter(X[row_ix, 0], X[row_ix, 1], label=label)
plt.legend()
plt.show()
輸出:

我想用 ,替換0, 1legend 。我來啦:catdog
for label, _ in counter.items():
row_ix = np.where(y == label)[0]
plt.scatter(X[row_ix, 0], X[row_ix, 1], label=['cat', 'dog'])
plt.legend()
plt.show()
輸出:

uj5u.com熱心網友回復:
您需要在for回圈的每次迭代中更改圖例標簽的值,一種可能性是使用zip
for item, animal in zip(counter.items(), ['cat', 'dog']):
row_ix = np.where(y == item[0])[0]
plt.scatter(X[row_ix, 0], X[row_ix, 1], label=animal)
plt.legend()
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452284.html
標籤:Python matplotlib 散点图
上一篇:熊貓條形圖沒有列出顏色
