【Seaborn】set_style関数による見た目の変更#

公開日

Seabornのset_style関数を用いてグラフの背景色や罫線などの見た目を変更する方法を解説します。set_style関数の最初の引数、またはstyle引数にスタイルを文字列で渡します。指定できるスタイルは以下の通りです。

  • ticks: 白色の背景、罫線なし、目盛り線あり(デフォルト)

  • darkgrid: 灰色の背景、罫線あり

  • whitegrid: 白色の背景、罫線あり

  • dark: 灰色の背景、罫線なし

  • white: 白色の背景、罫線なし

set_style関数で設定したスタイルは、それ以降のグラフにも継続して設定されます。スタイルを元に戻す場合、以下を実行してデフォルトのticksスタイルにします。

sns.set_style("ticks")

なお、sns.reset_defaults()関数を使用してスタイルを元に戻そうとした場合、rcParamsというMatplotlibの設定が全てデフォルトに戻され、Seabornの設定が消去されます。そのため、グラフのサイズなどがSeabornのデフォルト設定から変化してしまいますので推奨しません。

ticks#

デフォルトのスタイルであるticksを示します。軸に目盛り線が表示されます。表示するデータには、Seabornのirisデータセットを使用しています。

import seaborn as sns

df = sns.load_dataset("iris")
sns.scatterplot(data=df, x="sepal_length", y="petal_length")
<AxesSubplot:xlabel='sepal_length', ylabel='petal_length'>
../_images/bf06ccaee82e787dafba1521a7300d9c6de074466d49d43a72b78a271bb1a05c.png

darkgrid#

darkgridスタイルの例を以下に示します。灰色の背景、かつ罫線ありとなります。

sns.set_style("darkgrid")
sns.scatterplot(data=df, x="sepal_length", y="petal_length")
<AxesSubplot:xlabel='sepal_length', ylabel='petal_length'>
../_images/59bca8b9903529ad4292e23549beaec1844371b1ac06636a5e5c1e04392aca91.png

whitegrid#

whitegridスタイルの例を以下に示します。白色の背景、かつ罫線ありとなります。

sns.set_style("whitegrid")
sns.scatterplot(data=df, x="sepal_length", y="petal_length")
<AxesSubplot:xlabel='sepal_length', ylabel='petal_length'>
../_images/ad6c6dd9a21828b115c1928c8f20379faaa0accd96c06ff116c48e0b7d971874.png

dark#

darkスタイルの例を以下に示します。灰色の背景、かつ罫線なしとなります。

sns.set_style("dark")
sns.scatterplot(data=df, x="sepal_length", y="petal_length")
<AxesSubplot:xlabel='sepal_length', ylabel='petal_length'>
../_images/a9be8b78760853b940e977bbda1df957538c2ea943f1cb7d607fcac6e2bb9f78.png

white#

whiteスタイルの例を以下に示します。白色の背景、かつ罫線なしとなります。

sns.set_style("white")
sns.scatterplot(data=df, x="sepal_length", y="petal_length")
<AxesSubplot:xlabel='sepal_length', ylabel='petal_length'>
../_images/5a21250c1e9ce98f999b0406cc00a39d6dcdd88ad0635ddd6139861bceff7215.png