Sebastian
Sebastian

Reputation: 1960

Change boxplot legend's rectangles dimensions and legend text (Seaborn)

I'd like to change the text of the legend in a Seaborn boxplot, but when I do the little rectangles/icons that show the hue in the legend become really thin, so I'd like to control the dimension of these icons as well:

Before changing labels (default legend):

Before

After changing labels:

After

For reference, I'm changing labels using ax.legend(labels = [....])

Upvotes: 1

Views: 267

Answers (1)

r-beginners
r-beginners

Reputation: 35145

Based on the official reference example, the code to get the handler of the legend and add an arbitrary label name.

import seaborn as sns
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set3")
handler, label = ax.get_legend_handles_labels()
ax.legend(handler, [1,0])

enter image description here

Upvotes: 1

Related Questions