Python 3 – SeabornDistplot-Pythonチュートリアル

Python

目次

Seaborn distplotを使用すると、線が付いたヒストグラムを表示できます。 これは、あらゆる種類のバリエーションで表示できます。 SeabornをPythonプロットモジュールであるmatplotlibと組み合わせて使用​​します。

分布図は、観測値の単変量分布をプロットします。 distplot()関数は、matplotlib hist関数をseaborn kdeplot()およびrugplot()関数と組み合わせます。

関連コース: Matplotlibの例とビデオコース

Distplotの例

以下のプロットは、単純な分布を示しています。 random.randn()を使用してランダムな値を作成します。
これは、手動で値を定義する場合にも機能します。

1
2
3
4
5
6
7
import matplotlib.pyplot as plt
import seaborn as sns, numpy as np

sns.set(rc={"figure.figsize": (8, 4)}); np.random.seed(0)
x = np.random.randn(100)
ax = sns.distplot(x)
plt.show()

海生まれのdistplot

Distplotの例

distplotのあらゆる種類のバリエーションを表示できます。 pylabモジュールのsubplot()メソッドを使用して、4つのバリエーションを一度に表示します。

distplot()メソッドのパラメーターを変更することにより、まったく異なるビューを作成できます。 これらのパラメータをいじって、色や向きなどを変更できます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import matplotlib.pyplot as plt
import seaborn as sns, numpy as np
from pylab import *

sns.set(rc={"figure.figsize": (8, 4)}); np.random.seed(0)
x = np.random.randn(100)

subplot(2,2,1)
ax = sns.distplot(x)

subplot(2,2,2)
ax = sns.distplot(x, rug=False, hist=False)

subplot(2,2,3)
ax = sns.distplot(x, vertical=True)

subplot(2,2,4)
ax = sns.kdeplot(x, shade=True, color="r")

plt.show()

海生まれのdistplotの例

例をダウンロード

Seaborn dist

ヒストグラムでseabornの標準データセットも表示します。
これは大きなデータセットであるため、1つの列のみを使用します。

1
2
3
4
5
6
7
import matplotlib.pyplot as plt
import seaborn as sns

titanic=sns.load_dataset('titanic')
age1=titanic['age'].dropna()
sns.distplot(age1)
plt.show()

海生まれのdistplotの例

ディスプロットビン

ビンの数を変更したり、行を非表示にしたい場合は、それも可能です。
メソッドdistplot9)を呼び出すときは、ビンの数を渡して、行(kde)を非表示にするように指示できます。

1
2
3
4
5
6
7
import matplotlib.pyplot as plt
import seaborn as sns

titanic=sns.load_dataset('titanic')
age1=titanic['age'].dropna()
sns.distplot(age1,bins=30,kde=False)
plt.show()

海生まれのdistplotビン

海生まれのさまざまなプロット

以下の例は、他のいくつかの分布プロットの例を示しています。 grid(True)メソッド呼び出しを使用してグリッドをアクティブ化します。

1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt
import seaborn as sns

titanic=sns.load_dataset('titanic')
age1=titanic['age'].dropna()

fig,axes=plt.subplots(1,2)
sns.distplot(age1,ax=axes[0])
plt.grid(True)
sns.distplot(age1,rug=True,ax=axes[1])
plt.show()

海生まれのdistplotグリッド

matplotlibを初めて使用する場合は、 それなら私はこのコースを強くお勧めします。

例をダウンロード

Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です