Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-11-29 04:36:48

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 = [....])

Questioner
Sebastian Rivas
Viewed
0
r-beginners 2020-11-29 14:04:51

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