簡體   English   中英

Python pyplot圖例散點圖

[英]Python pyplot legend scatter

我有 5 個課程和一些我想要繪制的功能。 這是代碼

x_pts = X_test.iloc[:,col_1]
y_pts = X_test.iloc[:,col_2]
color_seq = y_test
plt.scatter(x_pts, y_pts, c=color_seq, cmap='viridis')
plt.xlabel(X_test.columns[col_1])
plt.ylabel(X_test.columns[col_2])
plt.show()

這導致以下圖像

陰謀

我現在想要每種顏色的圖例(例如,黃色 = 'class a' ,blue = 'class b',...)我能找到的唯一文檔是人們以不同的方式繪制每種顏色,這在我的特定情況下非常困難. 有沒有一種簡單的方法可以像這里的示例一樣顯示圖例

希望這可以幫助:

x = [1, 3, 4, 6, 7, 9]
y = [0, 0, 5, 8, 8, 8]
labels = ['A', 'B', 'C']
colors = [0, 0, 1, 2, 2, 2]
scatter = plt.scatter(x, y,c=colors, cmap='viridis')
plt.legend(handles=scatter.legend_elements()[0], labels=labels)
plt.show()

輸出: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM