Seaborn 标题错误 - AttributeError: 'FacetGrid' 对象没有属性 'set_title

DSo*_*thy 5 python seaborn

我首先使用以下代码创建了一个线图:

plot = sns.lineplot(data=tips,
             x="sex",
             y="tip",
             ci=50,
             hue="day",
             palette="Accent")
plot.set_title("Value of Tips Given to Waiters, by Days of the Week and Sex", fontsize=24, pad=30, fontdict={"weight": "bold"})
plot.legend("")
Run Code Online (Sandbox Code Playgroud)

我意识到它实际上是我需要的猫图,因此我将代码修改为以下内容:

plot = sns.catplot (data=tips,
             x="day",
             y="tip",
             kind='bar',
             ci=50,
             hue="sex",
             palette="Accent")
plot.set_title("Value of Tips Given to Waiters, by Days of the Week and Sex", fontsize=24, pad=30, fontdict={"weight": "bold"})
plot.legend("")
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下标题错误消息:'AttributeError:'FacetGrid' object has no attribute 'set_title''。

为什么我的标题不适用于猫图?

Stu*_*olf 5

当您调用 catplot 时,它返回一个 FacetGrid 对象,因此要更改标题并删除图例,您必须使用legend=函数内部的选项,并使用plot.fig.suptitle()

import seaborn as sns
tips = sns.load_dataset("tips")
plot = sns.catplot (data=tips,
             x="day",
             y="tip",
             kind='bar',
             ci=50,
             hue="sex",
             palette="Accent", legend=False)

plot.fig.suptitle("Value of Tips Given to Waiters, by Days of the Week and Sex",
                  fontsize=24, fontdict={"weight": "bold"})
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 它是seaborn包的一部分吗?```tips = sns.load_dataset("tips")``` (2认同)