Seaborn
boxplot
data visualization
Python
plotting

How to add a title to a Seaborn boxplot

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Adding a title to a Seaborn boxplot is straightforward once you remember that Seaborn draws on top of matplotlib. For a normal boxplot, you usually set the title on the Axes object. For grid-style plots such as catplot, the title belongs on the figure rather than on a single subplot.

Title a Standard Boxplot Through the Axes

sns.boxplot returns a matplotlib Axes object. The cleanest pattern is to keep that object and call set_title on it.

python
1import matplotlib.pyplot as plt
2import seaborn as sns
3
4sns.set_theme(style="whitegrid")
5tips = sns.load_dataset("tips")
6
7fig, ax = plt.subplots(figsize=(8, 5))
8sns.boxplot(data=tips, x="day", y="total_bill", ax=ax)
9ax.set_title("Total Bill by Day")
10
11plt.show()

That is the most direct answer for the typical question. It also scales well when your script creates more than one subplot, because you always know which axes you are modifying.

Style the Title Deliberately

Because the title is managed by matplotlib, all normal styling options still apply. You can set font size, weight, alignment, and padding.

python
1import matplotlib.pyplot as plt
2import seaborn as sns
3
4sns.set_theme(style="ticks")
5tips = sns.load_dataset("tips")
6
7fig, ax = plt.subplots(figsize=(9, 5))
8sns.boxplot(data=tips, x="day", y="tip", hue="sex", ax=ax)
9
10ax.set_title(
11    "Tip Distribution by Day and Gender",
12    fontsize=15,
13    fontweight="bold",
14    loc="left",
15    pad=12,
16)
17ax.set_xlabel("Day of Week")
18ax.set_ylabel("Tip Amount")
19
20plt.tight_layout()
21plt.show()

The important part is not the styling API itself. It is attaching the title to the correct plotting object so the code remains predictable as the figure grows more complex.

plt.title() Works, but It Is Less Explicit

For quick one-off scripts, plt.title() is acceptable because it targets the current axes.

python
1import matplotlib.pyplot as plt
2import seaborn as sns
3
4sns.boxplot(data=sns.load_dataset("tips"), x="day", y="total_bill")
5plt.title("Quick Boxplot Title")
6plt.show()

That style is fine in notebooks and small demos. In larger scripts, ax.set_title() is safer because it makes the target explicit. If several subplots exist, plt.title() can easily affect the wrong one if the current axes changed earlier in the code.

Figure-Level Titles for Faceted Plots

Some Seaborn APIs create a grid of plots. sns.catplot(kind="box") returns a FacetGrid, not a single Axes. In that case, a figure-level title is usually the right tool.

python
1import matplotlib.pyplot as plt
2import seaborn as sns
3
4g = sns.catplot(
5    data=sns.load_dataset("tips"),
6    x="day",
7    y="total_bill",
8    col="sex",
9    kind="box",
10    height=4,
11    aspect=1,
12)
13
14g.fig.suptitle("Total Bill by Day, Split by Gender", y=1.05)
15plt.show()

You can still title individual subplot axes, but if the goal is shared context for the whole grid, suptitle communicates that more clearly.

Avoid Clipped Titles in Saved Output

A title that looks fine on screen may get clipped in an exported image. This usually happens when the figure is tight and the title sits close to the canvas edge.

A practical workflow is:

  • use tight_layout() for normal axes titles
  • adjust y for suptitle
  • save with bbox_inches="tight" when needed
python
plt.tight_layout()
plt.savefig("boxplot.png", dpi=150, bbox_inches="tight")

Do not assume notebook rendering matches the final PNG or PDF. Check the exported artifact when the figure is going into a report or presentation.

Keep Titles Focused

A boxplot title should add context, not repeat what the axes already say. If the x-axis is day and the y-axis is total_bill, a title like Boxplot of total_bill by day is technically correct but not especially useful. A better title frames the chart in reader language, such as Total Bill by Day or Tip Distribution by Service Day.

That keeps the chart readable and avoids unnecessary clutter.

Common Pitfalls

A common mistake is searching for a title= argument on sns.boxplot. The function does not take one, so the title must be set after the plot is created.

Another problem is using plt.title() in multi-axes code and accidentally changing the wrong subplot. This is why keeping the returned Axes object is usually better.

Developers also forget that catplot produces a grid object, not a normal single axes. In those cases, g.fig.suptitle() is the correct place for an overall title.

Summary

  • For a standard Seaborn boxplot, store the returned axes and call ax.set_title().
  • 'plt.title() works for simple scripts but is less explicit.'
  • For catplot and other faceted figures, use g.fig.suptitle().
  • Style titles with normal matplotlib options such as fontsize, pad, and loc.
  • Check saved figures so titles are not clipped or overlapping the plot.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.