Seaborn: fontsize of relplot legend can't be specified.

Created on 29 May 2019  路  2Comments  路  Source: mwaskom/seaborn

Unlike most matplotlib legends, one cannot adjust the fontsize of the seaborn.relplot facetgrid's legend.

Per the advice in the accepted answer to How can I change the font size using seaborn FacetGrid?, I tried:

fg = sns.relplot(data=df, kind='line', x='Time', y=signal, hue='role', style='subsignal', dashes=True, linewidth=.5, alpha=.75, ax=None, ci='sd')
plt.setp(fg._legend.get_title(), fontsize=6)
plt.show()

But it didn't change the fontsize in the legend.

It'd be great if one could access and manipulate properties of a matplotlib-legend object from FacetGrid, or if FacetGrid's legend provided deterministically set-able properties like fontsize.

Most helpful comment

Hi @kusanagee ,

In matplotlib, parameters of visual features may be tweaked by updating the rcParams dictionary, dictating the appearance of many visual parameters. With seaborn you can update the plotting context to have the desired legend fontsize just before creating the relplot, see this example:

import seaborn as sns
import matplotlib.pyplot as plt

df=sns.load_dataset("fmri")
with sns.plotting_context(rc={"legend.fontsize":6}):
    g = sns.relplot(data=df, kind='line', x='timepoint', y="signal", hue='event')
    plt.show()

I hope this helps.

All 2 comments

Hi @kusanagee ,

In matplotlib, parameters of visual features may be tweaked by updating the rcParams dictionary, dictating the appearance of many visual parameters. With seaborn you can update the plotting context to have the desired legend fontsize just before creating the relplot, see this example:

import seaborn as sns
import matplotlib.pyplot as plt

df=sns.load_dataset("fmri")
with sns.plotting_context(rc={"legend.fontsize":6}):
    g = sns.relplot(data=df, kind='line', x='timepoint', y="signal", hue='event')
    plt.show()

I hope this helps.

Thanks @MaozGelbart

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vinay-jayaram picture vinay-jayaram  路  3Comments

bondarevts picture bondarevts  路  3Comments

chanshing picture chanshing  路  3Comments

sungshine picture sungshine  路  3Comments

ConstantinoSchillebeeckx picture ConstantinoSchillebeeckx  路  4Comments