如何更改 FacetGrid 图的图例字体大小?

时间:2021-06-25 16:48:32

标签: python seaborn

我正在尝试使用带有色调的 sns.displot() 绘制直方图。我正在尝试使用 ax.get_legend() 或 plt.legend() 调整每个直方图的图例大小。它告诉我 facegrid 没有图例的句柄。这是情节。谢谢

g=sns.displot(data,x=x,kind='hist', fill=True, hue=hue,palette=sns.color_palette('bright')[:4], height=15, aspect=1.5)

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以从 FacetGridsns.displot 一起返回的 FacetGrid.legend 访问图例。然后你可以像这样修改文本元素:

import seaborn as sns

tips = sns.load_dataset("tips")

g = sns.displot(data=tips, x="total_bill", hue="day")

# Legend title
g.legend.get_title().set_fontsize(20)

# Legend texts
for text in g.legend.texts:
    text.set_fontsize(20)

displot with larger legend text