1

I would like to manually add legends to my Seaborn plot.

For example, the image below is the plot I am trying to manually add a legend to:

r

I would like to add a legend:

{
"1":"High efficiency",
"2": "High performance",
"3": "Effective performance",
"4": "Relatively low speed",
"5": "Reduce performance",
"6": "Not recommeneded"
}
pjs
  • 18,696
  • 4
  • 27
  • 56
JA-pythonista
  • 1,225
  • 1
  • 21
  • 44

1 Answers1

1

Try:

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

enter image description here

Pygirl
  • 12,969
  • 5
  • 30
  • 43