Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit or remove facet labels of Seaborn displot

My question is about removing the facet labels from a displot(). I feel like they should be an attribute of the plot object but I can't quite dig them out/set them. Maybe I'm going about it all wrong.

I've got a faceted histogram plot. But I plan on incorporating this as one of many plots and so there won't be enough space in the final image to include the facet details in their current location, so I'd like to remove them from the subplot.

Code from the seaborn displot docs:

import seaborn as sns

sns.set_theme(style="darkgrid")
df = sns.load_dataset("penguins")
sns.displot(
    df, x="flipper_length_mm", col="species", row="sex",
    binwidth=3, height=3, facet_kws=dict(margin_titles=True),
)

This produces the plot:

enter image description here

I'd like to remove the entire species = ... from the facet titles.

like image 594
dcurrie27 Avatar asked Jan 24 '26 07:01

dcurrie27


2 Answers

You can use the method sns.FacetGrid.set_titles to turn off (or change the labels of) column and row titles.

I would caution against this, unless you are setting the col_order parameter so that you know which axes belongs to each particular variable.

import seaborn as sns

sns.set_theme(style="darkgrid")
df = sns.load_dataset("penguins")

g = sns.displot(
    df, x="flipper_length_mm", col="species", row="sex",
    binwidth=3, height=3, facet_kws=dict(margin_titles=True),
)
g.set_titles(col_template="")
# This will also remove the row labels
# g.set_titles(col_template="", row_template="")

Histogram with column titles removed.


When not using margin_titles using g.set_titles(template="") will remove the entire top title.

like image 169
Alex Avatar answered Jan 25 '26 21:01

Alex


Try something like this:

import seaborn as sns

sns.set_theme(style="darkgrid")
df = sns.load_dataset("penguins")
s=sns.displot(
    df, x="flipper_length_mm", col="species", row="sex",
    binwidth=3, height=3, facet_kws=dict(margin_titles=True),
)

s.set_titles("{col_name}");
like image 43
Adam Aslam Avatar answered Jan 25 '26 19:01

Adam Aslam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!