Edit or remove facet labels of Seaborn displot

Question:

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.

Asked By: dcurrie27

||

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.

Answered By: Alex
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.