簡體   English   中英

如何使用 Seaborn 手動添加圖例

[英]How to manually add a legend with Seaborn

我想手動將legends添加到我的Seaborn plot

例如,下圖是我嘗試手動添加圖例的圖:

r

我想添加一個圖例:

{
"1":"High efficiency",
"2": "High performance",
"3": "Effective performance",
"4": "Relatively low speed",
"5": "Reduce performance",
"6": "Not recommeneded"
}

嘗試:

import seaborn as sns
import matplotlib.patches as mpatches

x=[1,1000,1001]
y=[200,300,400]
sns.set_context(rc={"figure.figsize": (8, 4)})
nd = np.arange(3)
width=0.8
plt.xticks(nd+width/2., ('1','1000','1001'))
plt.xlim(-0.15,3)

ax = sns.barplot(x=x,y=y)
colors = ['r', 'g', 'b']

labels = {
    1:'x',
    1000:'y',
    1001:'z'
}
handles = []
for col, lab, patch in zip(colors, x, ax.axes.patches):
    patch.set_color(col)
    handles.append(mpatches.Patch(color=col, label=labels[lab]))

ax.legend(handles=handles) 
plt.show()

在此處輸入圖片說明

暫無
暫無

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

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