15
12

More than 5 years have passed since last update.

seabon.jointplotに相関係数とp値を書き込む

Last updated at Posted at 2019-03-06

seaborn.jointplotで同時分布を書く

  • Seabornモジュールの jointplot は同時分布(ヒストグラム付き散布図)を簡単に描ける

例えばirisデータを使うと、

import seaborn as sns
df = sns.load_dataset('iris')
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"])

image.png

こんな感じで簡単にグラフが描ける。

相関係数をグラフ上に書き込む

  • 調べていると、少し古い記事ではjointplotを使ったときにピアソンの相関係数とp値がグラフ内に自動的に書き込まれている。羨ましい。それ欲しい。
  • seabornの新しいバージョン(0.9.0)では jointplot のデフォルト引数を見ると、 stat_func = None になっている上、公式で非推奨(Deprecated)になっている。
  • 仕方がないのでscipyのstatsを使ってピアソンの相関係数とp値を計算し、annotate(意:注釈)に渡して書き加える→.annotate(stats.pearsonr)

※もちろんpeasonr以外のscipy.statsで計算できる他の統計値も使える

from scipy import stats
sns.jointplot(x=df["sepal_length"],y=df["sepal_width"]).annotate(stats.pearsonr)

image.png

  • 無事書き加えられた。
  • しかし、annotateの枠が邪魔だから消す→ frameon=False
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"]).annotate(stats.pearsonr,frameon=False)

image.png

完成。


参考
https://kite.com/python/docs/seaborn.axisgrid.JointGrid.annotate

15
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12