Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn title error - AttributeError: 'FacetGrid' object has no attribute 'set_title

Tags:

python

seaborn

I created a lineplot graph to begin with using the following code:

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("")

I have realised that its actually a catplot chart that I need so I amended the code to the following:

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("")

However I am getting the following error message with the title: 'AttributeError: 'FacetGrid' object has no attribute 'set_title''.

Why is my title not working for the catplot chart?

like image 235
DSouthy Avatar asked Oct 08 '20 08:10

DSouthy


1 Answers

When you call catplot, it returns a FacetGrid object, so to change the the title and remove legend, you have to use the legend= option inside the function, and also use 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"})

enter image description here

like image 154
StupidWolf Avatar answered Sep 25 '22 13:09

StupidWolf