Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 38 ยท Styling and Themes

Axis Labels, Titles, Figure Size and Legends

Set titles, axis labels and figure size for axes-level and figure-level seaborn plots, rotate ticks, and move the legend outside the plot area.

A chart without a title, clear axis labels and a readable legend is only half finished. Seaborn picks column names for labels and puts the legend wherever it fits, which is fine for exploring and rarely good enough for a report. The trick to customising seaborn is to know which kind of function you called, because the answer to "how do I set the title?" depends on it.

This lesson shows the right tool for both kinds, then tackles the question everyone asks sooner or later: how to put the legend outside the plot.

Two kinds of functions, two ways to customise

Which function did you call?

Axes-level scatterplot, lineplot, barplot, boxplot, histplot, heatmap
Figure-level relplot, catplot, displot, lmplot, pairplot, jointplot

Axes-level returns an Axes. Figure-level returns a grid.

1 / 2

Axes-level: use the returned Axes

Axes-level functions return a Matplotlib Axes. Set everything on it, in one call with ax.set(...) or with individual methods. The figure size comes from plt.subplots(figsize=(w, h)) created before the plot.

Python Title, labels and size on an Axes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

font_scale in set_theme() scales every piece of text at once, which is faster than touching each label. Use ax.set_xlabel("...", fontsize=12) for a single label. To change the figure size of an existing figure, call fig.set_size_inches(8, 5).

Figure-level: use the grid

Figure-level functions make their own figure, so plt.subplots and ax= do not apply. You size the panels with height and aspect, label with the grid's methods and title the whole figure with suptitle:

Python Labels and title on a figure-level plot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Useful grid methods: set_axis_labels, set_xlabels, set_ylabels, set_titles, set_xticklabels(rotation=45), set(...) for anything else, and g.axes for a NumPy array of the underlying Axes when you need Matplotlib directly. Total figure size is roughly height * aspect * number_of_columns wide.

Rotating and trimming tick labels

Long category names collide. Rotate them, or flip the plot so the names run vertically:

Python Long tick labels: rotate or flip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Horizontal bars are almost always easier to read when names are long. fig.tight_layout() stops labels being cut off.

Putting the legend outside the plot

By default the legend sits inside the axes, where it can cover data. How you move it depends on the function type.

Axes-level: call sns.move_legend() (seaborn 0.11.2+), or Matplotlib's ax.legend(...) with bbox_to_anchor:

Python Legend outside, axes-level

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

bbox_to_anchor=(1.02, 1) pins the legend's upper-left corner just right of the axes (x = 1 is the right edge). tight_layout() then makes room; if the legend is still clipped when you save, use fig.savefig("plot.png", bbox_inches="tight").

Figure-level: the legend belongs to the grid and is already outside. Move it with sns.move_legend(g, ...), or place it below the plot:

Python Moving a figure-level legend

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For figure-level plots you can also pass facet_kws={"legend_out": False} to bring the legend inside, or remove it with g.legend.remove().

Legend extras

  • legend=False in the plotting call hides it (and legend="full" shows every level of a numeric hue).
  • Change the order with hue_order=[...], not by editing the legend.
  • Change legend text by recoding the column (df["group"] = df["group"].str.title()) before plotting; that keeps data, legend and tooltips in sync.
  • Check colors and styles in Matplotlib for marker and linestyle details.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You call sns.scatterplot(...). How do you set its title?

    Axes-level functions return an Axes, so use Matplotlib's Axes methods.
  2. 2. How do you size a figure-level plot?

    Figure-level functions create their own figure and size each panel with height and aspect.
  3. 3. Which function moves a seaborn legend, for both Axes and grids?

    move_legend accepts an Axes or a grid and takes the same keywords as Matplotlib's legend().
  4. 4. What does font_scale=1.3 in set_theme do?

    It scales every font element of the theme at once.

Summary

  • Axes-level: ax.set(title=, xlabel=, ylabel=) and plt.subplots(figsize=...).
  • Figure-level: g.set_axis_labels(), g.set_titles(), g.figure.suptitle(), height and aspect.
  • Long labels: rotate them or switch to horizontal bars.
  • sns.move_legend() with bbox_to_anchor puts the legend outside; font_scale scales all text.

Next: Themes, Styles and Contexts