GiusWestsideDS
GiusWestsideDS

Reputation: 37

How to use Matplotlib rcParams with Seaborn

I'm writing a custom matplotlib stylesheet for my company. Among other things I'm trying to change the colors of the boxplot lines. The following example changes the rcParams using a dictionary. The standard plot built using matplotlib has the right colors, while it seems that only some parameters are changed in the seaborn plot. How can I force seaborn to use my stylesheet?

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df_penguins = pd.read_csv(
    "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
)
ex = {
        'boxplot.boxprops.color': 'hotpink',
        'boxplot.notch': True,
        'boxplot.patchartist': False,
        'boxplot.showbox': True,
        'boxplot.showcaps': True,
        'boxplot.showfliers': True,
        'boxplot.showmeans': False,
        'boxplot.vertical': True,
        'boxplot.whiskerprops.color': 'hotpink',
        'boxplot.whiskerprops.linestyle': '--',
        'boxplot.whiskerprops.linewidth': 1.0,
        'boxplot.whiskers': 1.5,
    }

plt.rcParams.update(**ex)
fig, (ax1, ax2) = plt.subplots(
    ncols=2, 
    sharey=True,
    figsize=plt.figaspect(0.5)
)
sns.boxplot(data=df_penguins, y="body_mass_g", ax=ax1)

ax2.boxplot(df_penguins.body_mass_g.dropna())
plt.show()

enter image description here

Upvotes: 1

Views: 1399

Answers (1)

buhtz
buhtz

Reputation: 12152

Different from @mwaskom 's comment Seaborn does use matplotlib's rcParams under the hood.

By default you activate the seaborn default them via sns.set_theme() which has a direct effect on rcParams. You can overwrite the defaults using the rc argument in that function.

Sidenote: It is possible but not recommended to mix matplotlib and seaborn imports. In short: Don't import matplotlib when importing seaborn.

I updated your code.

#!/usr/bin/env python3
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df_penguins = pd.read_csv(
    "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
)
ex = {
        'boxplot.boxprops.color': 'hotpink',
        'boxplot.notch': True,
        'boxplot.patchartist': False,
        'boxplot.showbox': True,
        'boxplot.showcaps': True,
        'boxplot.showfliers': True,
        'boxplot.showmeans': False,
        'boxplot.vertical': True,
        'boxplot.whiskerprops.color': 'hotpink',
        'boxplot.whiskerprops.linestyle': '--',
        'boxplot.whiskerprops.linewidth': 1.0,
        'boxplot.whiskers': 1.5,
    }

# This loads the Seaborn default theme settings
# and overwrites them with your values from "ex"
sns.set_theme(rc=ex)

plot = sns.boxplot(data=df_penguins, y="body_mass_g")
plot.figure.show()

enter image description here

Be aware that not all of your settings from ex do have an effect here. For example, the hotpink. But I'm sure this is not a problem with my solution. For example boxplot.notch does work.

So I'm sorry that this only answers how to modify the rcParams via Seaborn but not how to modify specific aspects of your theme.

Upvotes: 2

Related Questions