How to change the margin title color in a seaborn FacetGrid

Viewed 580

Using Seaborn Facet Grids, how can I change the color of the margin title only? Note that g.set_titles(color = 'red') changes both titles.

p = sns.load_dataset('penguins')
sns.displot(data=p, x='flipper_length_mm', 
            col='species', row='sex', 
            facet_kws=dict(margin_titles=True)
           )

enter image description here

1 Answers

The sns.FacetGrid class has a private attribute _margin_titles_texts which is a list containing the matplotlib.text.Annotation objects that make the margin titles. We can iterate through those and call the set_color method on each to change the text colour.

import seaborn as sns

p = sns.load_dataset("penguins")
grid = sns.displot(
    data=p,
    x="flipper_length_mm",
    col="species",
    row="sex",
    facet_kws=dict(margin_titles=True),
)
for margin_title in grid._margin_titles_texts:
    margin_title.set_color("red")

sns.displot of penguins data with red margin titles

Related