我创建了一个线图,首先使用以下代码:
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("")
我已经意识到它实际上是我所需要的猫形图,因此我将代码修改为以下内容:
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("")
但是,我收到以下错误消息,标题为:'AttributeError:'FacetGrid'对象没有属性'set_title'。
为什么我的标题在猫形图上不起作用?
答案 0 :(得分:1)
调用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"})