How to add title to seaborn boxplot
Seaborn box plot returns a matplotlib axes instance. Unlike pyplot itself, which has a method plt.title()
, the corresponding argument for an axes is ax.set_title()
. Therefore you need to call
sns.boxplot('Day', 'Count', data= gg).set_title('lalala')
A complete example would be:
import seaborn as snsimport matplotlib.pyplot as plttips = sns.load_dataset("tips")sns.boxplot(x=tips["total_bill"]).set_title("LaLaLa")plt.show()
Of course you could also use the returned axes instance to make it more readable:
ax = sns.boxplot('Day', 'Count', data= gg)ax.set_title('lalala')ax.set_ylabel('lololo')
sns.boxplot() function returns Axes(matplotlib.axes.Axes) object. please refer the documentationyou can add title using 'set' method as below:
sns.boxplot('Day', 'Count', data=gg).set(title='lalala')
you can also add other parameters like xlabel, ylabel to the set method.
sns.boxplot('Day', 'Count', data=gg).set(title='lalala', xlabel='its x_label', ylabel='its y_label')
There are some other methods as mentioned in the matplotlib.axes.Axes documentaion to add tile, legend and labels.
Try adding this at the end of your code:
import matplotlib.pyplot as pltplt.title('add title here')