简体   繁体   中英

How to change the margin title color in a seaborn FacetGrid

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

带有红色边距标题的企鹅数据的 sns.displot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM