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 reproduc
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
One would hence also need to adjust the figure margins a bit,
g.fig.subplots_adjust(top=0.9,right=0.7)
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"])
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.