217

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.

It seems pretty googleable, but I haven't been able to find something online that works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
itstoocold
  • 2,385
  • 2
  • 12
  • 15

6 Answers6

317

A 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')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 6
    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
  • 3
    AttributeError: 'FacetGrid' object has no attribute 'set_title' – Dumb ML Aug 20 '20 at 15:04
  • @DumbML are you sure you were using a `boxpot`? This doesn't work with every graph generating method of seaborn. Particularly, I tried this with `lmplot` and it didn't work. (Same error) – Kelly Brower Jul 19 '22 at 21:21
64

The sns.boxplot() function returns an Axes(matplotlib.axes.Axes) object. Please refer the documentation.

You can add a title using 'set' method as below:

sns.boxplot('Day', 'Count', data=gg).set(title='lalala')

You can also add other parameters, like xlabel and 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 documentation to add tile, legend and labels.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
akhil penta
  • 970
  • 9
  • 13
41

Try adding this at the end of your code:

import matplotlib.pyplot as plt

plt.title('add title here')
Stefano Potter
  • 3,467
  • 10
  • 45
  • 82
16

For a single boxplot:

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

For more boxplots in the same plot:

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

For example,

import seaborn as sb
myarray = [78.195229, 59.104538, 19.884109, 25.941648, 72.234825, 82.313911]
sb.boxplot(data=myarray).set_title('myTitle')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shrm
  • 426
  • 4
  • 8
8

.set_title('') can be used to add a title to a Seaborn plot:

import seaborn as sb
sb.boxplot().set_title('Title')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kranthi
  • 452
  • 4
  • 3
0

Try this:

import seaborn as sns
g = sns.catplot(x='Day', y='Count', data=gg)
g.set_title('LaLa')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tayyab
  • 1
  • 2
  • 2
    Re *"Try this"*: An explanation would be in order. E.g., what is the idea/gist? How is it different from previous answers? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/72836377/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 13 '22 at 12:05