【问题标题】:How to manually add a legend with Seaborn如何使用 Seaborn 手动添加图例
【发布时间】:2020-06-27 13:57:58
【问题描述】:

我想手动将legends 添加到我的Seaborn plot

例如,下图是我尝试手动添加图例的图:

我想添加一个图例:

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

【问题讨论】:

标签: python pandas data-visualization seaborn


【解决方案1】:

试试:

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()

【讨论】:

    猜你喜欢
    • 2021-03-24
    • 2015-11-22
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多