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)
)
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")