198

Seems pretty Googleable but haven't been able to find something online that works.

I've tried both sns.boxplot('Day', 'Count', data= gg).title('lalala') and sns.boxplot('Day', 'Count', data= gg).suptitle('lalala'). None worked. I think it might be because I'm also working with matplotlib.

SuperStormer
  • 4,531
  • 5
  • 20
  • 32
itstoocold
  • 2,177
  • 2
  • 10
  • 14

5 Answers5

298

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 sns
import matplotlib.pyplot as plt

tips = 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')
ImportanceOfBeingErnest
  • 289,005
  • 45
  • 571
  • 615
  • 5
    its a shame `set_title()` and similar functions do not `return self`, that would be neat. – Laurens Koppenol Aug 29 '19 at 11:33
  • @LaurensKoppenol Matplotlib's credo is to return the object that the method creates or manipulates. This is a question of flexibility; and matplotlib explicitely wants to give users this flexibility. More high-level APIs that sit on top of matplotlib often decide to allow for chaining, but in those cases you have problems manipulating the underlying objects when wanting some non-standard behaviour. – ImportanceOfBeingErnest Aug 29 '19 at 11:41
  • when combining the various interfaces matplotlib has I definitely agree – Laurens Koppenol Aug 29 '19 at 11:45
  • 2
    AttributeError: 'FacetGrid' object has no attribute 'set_title' – Dumb ML Aug 20 '20 at 15:04
59

sns.boxplot() function returns Axes(matplotlib.axes.Axes) object. please refer the documentation you 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.

akhil penta
  • 850
  • 6
  • 13
41

Try adding this at the end of your code:

import matplotlib.pyplot as plt

plt.title('add title here')
Stefano Potter
  • 3,125
  • 6
  • 43
  • 72
16

For a single boxplot:

import seaborn as sb
sb.boxplot(data=Array).set_title('Title')

For more boxplot in the same plot:

import seaborn as sb
sb.boxplot(data=ArrayofArray).set_title('Title')

e.g.

import seaborn as sb
myarray=[78.195229, 59.104538, 19.884109, 25.941648, 72.234825, 82.313911]
sb.boxplot(data=myarray).set_title('myTitle')
Shrm
  • 368
  • 3
  • 8
7

.set_title('') can be used to add title to Seaborn Plot

import seaborn as sb
sb.boxplot().set_title('Title')
Kranthi
  • 352
  • 3
  • 3