lhk
lhk

Reputation: 30006

seaborn barplot with labels for x values (and no hue)

My dataframe contains two columns, I would like to plot their values in a barplot. Like this:

import seaborn as sns

# load sample data and drop all but two columns
tips = sns.load_dataset("tips")
tips= tips[["day", "total_bill"]]

sns.set(style="whitegrid")
ax = sns.barplot(x="day", y="total_bill", data=tips)

barplot

On top of this barplot, I would also like to add a legend with labels for each x value. Seaborn supports this, but as far as I can see, it works only when you specify a hue argument. Each label in the legend then corresponds to a hue value.

Can I create a legend with explanations for the x values? This might be a confusing question. I don't want to rename the label for the axis or the ticks along the axis. Instead, I would like to have a separate legend with additional explanations. My bars give me some nice space to put this legend and the explanations would be too long to have them as ticks.

Upvotes: 1

Views: 1263

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150725

Is this what you want:

sns.set(style="whitegrid")
ax = sns.barplot(x="day", y="total_bill", data=tips)

ax.legend(ax.patches, ['1','2','3','Something that I can\'t say'], loc=[1.01,0.5])

Output:

enter image description here

Upvotes: 1

Related Questions