• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / How to Change the Position of Legend in Seaborn

How to Change the Position of Legend in Seaborn

August 27, 2021 by cmdlinetips

Seaborn v0.11.2 is here. It is a minor release that fixes issues and also has a few features.

One of the useful features is the new convenient function “move_legend()” to change the position of legend in Seaborn. Before Seaborn v0.11.2, Matplotlib’s plt.legend() has been the go to function to change the position of legend in a plot made with Seaborn. Utilizing bbox_to_anchor as argument to legend() function we can place legend outside the plot.

Now, with move_legend() function available in Seaborn v0.11.2 it is easier to place the legend where you want. Seaborn v0.11.2 release notes suggest that move_legend()

function should be preferred over calling ax.legend with no legend data, which does not reliably work across seaborn plot types.

Load Libraries and Data

Let us load Seaborn and Pandas to make plots with Seaborn.

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

We will use Penguin data from Seaborn’s built-in datasets using load_dataset() function.

penguins = sns.load_dataset("penguins")

In order to make the plot and text on the plot clearly legible we will use set_context() function to set “talk” mode for figure sizes.

sns.set_context("talk", font_scale=1)

Seaborn plot with default legend position

First, let us make a scatterplot with Seaborn’s scatterplot() function with default legend position.

plt.figure(figsize=(8,6))
sns.scatterplot(data=penguins, 
                     x="bill_length_mm",
                     y="flipper_length_mm",
                     hue="species")
plt.xlabel("Bill Length")
plt.ylabel("Flipper Length")
plt.savefig("Seaborn_plot_with_default_legend_position.png",
                    format='png',dpi=150)

By default Seaborn tries to find the right place for legend.

Seaborn plot with default legend position
Seaborn plot with default legend position

Change Legend Position to lower right with Seaborn move_legend

Sometimes you might want to change the legend position to a place that is more suitable for the plot you are making. With Seaborn’s move_legend() function, we can easily change the position to common legend places. For example, to move the legend to lower right of Seaborn plot we specify “lower right” to move_legend() function.

to place the legend in a

plt.figure(figsize=(8,6))
ax = sns.scatterplot(data=penguins, 
                     x="bill_length_mm",
                     y="flipper_length_mm",
                     hue="species")
sns.move_legend(ax, "lower right")
plt.xlabel("Bill Length")
plt.ylabel("Flipper Length")
plt.savefig("Seaborn_move_legend_to_lower_right.png",
                    format='png',dpi=150)

Note now the legend is moved to the lower left corner of the plot.

Seaborn move legend to lower right

Seaborn move legend to lower right

In addition to “lower right”, we can use other positions that are tyopically available with ax.legend() function. Here are the list of the positions available to move the legend.

  1. upper right
  2. upper left
  3. lower left
  4. lower right
  5. right
  6. center left
  7. center right
  8. lower center
  9. upper center
  10. center

Change Legend Position to top of the plot with Seaborn move_legend

One of the ways I often like to have the legend is on top of the plot, just outside the plot. We can use bbox_to_anchor argument inside move_legend() function to move the legend out side the plot.

plt.figure(figsize=(8,6))
ax = sns.scatterplot(data=penguins, 
                     x="bill_length_mm",
                     y="flipper_length_mm",
                     hue="species")
sns.move_legend(
    ax, "lower center",
    bbox_to_anchor=(.5, 1),
    frameon=False,
)
plt.xlabel("Bill Length")
plt.ylabel("Flipper Length")
plt.savefig("Seaborn_move_legend_to_the_top_of_plot.png",
                    format='png',dpi=150)

Here we have successfully moved the legend to outside on top of the plot. However, we can see that elements of the legend group are arranged in a column.

Seaborn move legend to the top of plot
Seaborn move legend to the top of plot

A better way to have the legend on top of the plot is to have in a single row without the title for legend. With ncol and title argument options we can move the legend to top of the plot (outside in a single row).

plt.figure(figsize=(8,6))
ax = sns.scatterplot(data=penguins, 
                     x="bill_length_mm",
                     y="flipper_length_mm",
                     hue="species")
sns.move_legend(
    ax, "lower center",
    bbox_to_anchor=(.5, 1),
    ncol=3,
    title=None,
    frameon=False,
)
plt.xlabel("Bill Length")
plt.ylabel("Flipper Length")
plt.savefig("Seaborn_move_legend_to_the_top_of_plot_in_a_row.png",
                    format='png',dpi=150)
Seaborn Legend in a row on top of the  plot
Seaborn Legend in a row on top of the plot

Change Legend Position to bottom of the plot with Seaborn move_legend

We can use the same approach to move the legend to the bottom of the plot outside and have the legend in a single row without legend title.

plt.figure(figsize=(8,6))
ax = sns.scatterplot(data=penguins, 
                     x="bill_length_mm",
                     y="flipper_length_mm",
                     hue="species")
sns.move_legend(
    ax, "lower center",
    bbox_to_anchor=(0.5, -.3),
    ncol=3,
    title=None, frameon=False,
)
plt.xlabel("Bill Length")
plt.ylabel("Flipper Length")
plt.savefig("Seaborn_move_legend_to_the_bottom_of_plot.png",
                    format='png',dpi=150)
Seaborn move legend at the bottom of plot in a single row
Seaborn move legend at the bottom of plot in a single row

Change Two Legend Group’s Position to the top of the plot with Seaborn move_legend

We can also move the multiple legend groups to the top, with each group in their own column.

plt.figure(figsize=(8,6))
ax = sns.scatterplot(data=penguins, 
                     x="bill_length_mm",
                     y="flipper_length_mm",
                     hue="species",
                    style="sex")
sns.move_legend(
    ax, "lower center",
    bbox_to_anchor=(.5, 1),
    ncol=2,
    title=None, frameon=False,
)
plt.xlabel("Bill Length")
plt.ylabel("Flipper Length")
plt.savefig("Seaborn_move_legend_to_the_top_with_two_groups.png",
                    format='png',dpi=150)

Seaborn move two legend group
Seaborn move two legend group

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

ggplot2 change legend title with guides()How To Change Legend Title in ggplot2? Seaborn Version 0.11.0 is HereSeaborn Version 0.11.0 is here with displot, histplot and ecdfplot Seaborn Trick Quick Gradient PaletteLesser known Seaborn Tips and Tricks Default ThumbnailHow To Move a Column to First Position in Pandas DataFrame?

Filed Under: Python Tagged With: Seaborn change legend position, Seaborn move legend

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version