よく使うのでメモ。
データのロード
import seaborn as sns data = sns.load_dataset('planets')
ロードしたデータの形式は以下に形。
method | number | orbital_period | mass | distance | year | |
---|---|---|---|---|---|---|
0 | Radial Velocity | 1 | 269.300 | 7.10 | 77.40 | 2006 |
1 | Radial Velocity | 1 | 874.774 | 2.21 | 56.95 | 2008 |
2 | Radial Velocity | 1 | 763.000 | 2.60 | 19.84 | 2011 |
3 | Radial Velocity | 1 | 326.030 | 19.40 | 110.62 | 2007 |
4 | Radial Velocity | 1 | 516.220 | 10.50 | 119.47 | 2009 |
プロット
import matplotlib.pyplot as plt import seaborn as sns import numpy as np TIMELEN = 1000 x = np.linspace(0, 20, TIMELEN) y = np.cumsum(np.random.randn(TIMELEN, 9), 0) plt.plot(x, y) plt.legend('ABCDEFGHI', ncol=3, loc='upper left');
棒グラフ
単純なカウント
ax = sns.factorplot("year", data=data, aspect=2,kind="count") ax.set_xticklabels(step=5)
特定のカラムの数のカウント(group by)
ax = sns.factorplot("year", data=data, aspect=2.0, kind='count',hue='method') ax.set_ylabels('Number of Planets Discovered')
散布図
二変数間での散布図
ax = sns.jointplot("mass", "distance", data, kind='hex') ax.ax_joint.plot(np.linspace(0, 16),np.linspace(0, 16), ':k')
二変数間の散布図を同時に複数
ax = sns.PairGrid(data, vars=['mass', 'distance', 'year'], hue='number').map(plt.scatter, alpha=0.8).add_legend()