3

I am trying to find guidance on how to control and customize the legend in Seaborn plots but I can not find any.

To make the issue more concrete I provide a reproducible example:

surveys_by_year_sex_long

    year    sex wgt
0   2001    F   36.221914
1   2001    M   36.481844
2   2002    F   34.016799
3   2002    M   37.589905

%matplotlib inline
from matplotlib import *
from matplotlib import pyplot as plt
import seaborn as sn

sn.factorplot(x = "year", y = "wgt", data = surveys_by_year_sex_long, hue = "sex", kind = "bar", legend_out = True,
             palette = sn.color_palette(palette = ["SteelBlue" , "Salmon"]), hue_order = ["M", "F"])
plt.xlabel('Year')
plt.ylabel('Weight')
plt.title('Average Weight by Year and Sex')

enter image description here

In this example I would like to be able to define M as Male and F as Female and instead of sex to have Sex as title of the legend.

Your advice will be appreciated.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
rf7
  • 1,993
  • 4
  • 21
  • 35

2 Answers2

8

First, to access the legend created by seaborn needs to be done via the seaborn call.

g = sns.factorplot(...)
legend = g._legend

This legend can then be manipulated,

legend.set_title("Sex")
for t, l in zip(legend.texts,("Male", "Female")):
    t.set_text(l)

The result is not totally pleasing because the strings in the legend are larger than previously, hence the legend would overlap the plot

enter image description here

One would hence also need to adjust the figure margins a bit,

g.fig.subplots_adjust(top=0.9,right=0.7)

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 4
    As a side note, this solution will work only for plotting functions that use Figure-level interface. For other functions, for example, `sns.boxplot()` the way around is to declare and call `matplotlib` axis object directly. – Light_B Aug 28 '19 at 15:18
6

I've always found changing labels in seaborn plots once they are created to be a bit tricky. The easiest solution seems to be to change the input data itself, by mapping the values and column names. You can create a new dataframe as follows, then use the same plot commands.

data = surveys_by_year_sex_long.rename(columns={'sex': 'Sex'})
data['Sex'] = data['Sex'].map({'M': 'Male', 'F': 'Female'})
sn.factorplot(
    x = "year", y = "wgt", data = data, hue = "Sex",
    kind = "bar", legend_out = True,
    palette = sn.color_palette(palette = ["SteelBlue" , "Salmon"]),
    hue_order = ["Male", "Female"])

Updated names

Hopefully this does what you need. The potential problem is that if the dataset is large, creating a whole new dataframe in this manner adds some overhead.

Simon Bowly
  • 1,003
  • 5
  • 10