Reading Time: 4 minutes

[ad_1]

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

[ad_2]

Source link

Spread the Word!