Gareth Howell
Gareth Howell

Reputation: 25

How to access/change boxplot features in a Seaborn catplot?

I have a seaborn box plot (constructed as a catplot with kind=box) and it basically looks how I want it to except I want to change some of the features of the box plot (linewidth around the boxes and the gap in between the dodged/hue boxes).

There are parameters to do this if I use sns.boxplot (gap=x etc) but these aren't recognised if I enter them in the sns.catplot function.

I'm guessing I need to access some keywords for the underlying boxplot function but I can't work out how to do it.

NOTE = if I plot as sns.boxplot I run into other problems with the legend

I am creating a catplot (kind=box) to show relationships between a category (cat1), float (var1) and integer (var2), this is the code:

g = sns.catplot(data = dataframe,
                        x="var1",
                        y="var2",
                        hue="cat1",
                        kind="box",
                        height=3.5,
                        aspect=1,
                        showfliers=False,
                        whis=(2, 98),
                        width=0.5,
                        legend=False,
                        )

g_eff.fig.get_axes()[0].legend(loc='lower left')

graph image

Upvotes: 0

Views: 266

Answers (1)

mwaskom
mwaskom

Reputation: 48992

catplot with kind="box" accepts all of the additional formatting kwargs that boxplot does. e.g. with the specific customizations mentioned in your question:

sns.catplot(tips, x="day", y="total_bill", hue="sex", linewidth=.5, gap=.1, kind="box")

enter image description here

The gap parameter was added in v0.13 so if you're finding that it's not working when you try maybe that is because you have an older version locally?

Note that boxplot (and hence catplot with kind="box") exposes more additional customization through parameters with unconventional (for seaborn) names such as boxprops, capprops, etc. because these are parameters of the underlying matplotlib function. You should be able to use them to achieve a very high level of customization (higher in v0.13 than in previous versions as the logic was reworked to honor the user's parameterization over seaborn's default customization in most places).

Upvotes: 0

Related Questions