Seaborn 2D 히스토그램에 정규 분포 추가

Nov 27 2020

seaborn에서 히스토그램을 가져와 정규 분포를 추가 할 수 있습니까?

문서 에서이 산점도 및 히스토그램과 같은 것이 있다고 가정 해보십시오 .

import seaborn as sns
penguins = sns.load_dataset("penguins")
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm");
plt.savefig('deletethis.png', bbox_inches='tight')

아래 이미지와 같이 분포를 측면에 겹쳐 놓을 수 있습니까?

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm

x = np.random.normal(size=100000)

# Plot histogram in one-dimension
plt.hist(x,bins=80,density=True)
xvals = np.arange(-4,4,0.01)
plt.plot(xvals, norm.pdf(xvals),label='$N(0,1)$')
plt.legend();

답변

2 RuthgerRighart Nov 27 2020 at 21:40

다음은 분포를 표시하는 커널 밀도 추정치를 제공합니다 (정상인 경우).

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot_joint(sns.scatterplot, s=100, alpha=.5)
g.plot_marginals(sns.histplot, kde=True)

다음은 좌표축의 히스토그램에 정규 분포를 중첩합니다.

import seaborn as sns
import numpy as np
import pandas as pd
from scipy.stats import norm

df1 = penguins.loc[:,["bill_length_mm", "bill_depth_mm"]]

axs = sns.jointplot("bill_length_mm", "bill_depth_mm", data=df1)
axs.ax_joint.scatter("bill_length_mm", "bill_depth_mm", data=df1, c='r', marker='x')

axs.ax_marg_x.cla()
axs.ax_marg_y.cla()
sns.distplot(df1.bill_length_mm, ax=axs.ax_marg_x, fit=norm)
sns.distplot(df1.bill_depth_mm, ax=axs.ax_marg_y, vertical=True, fit=norm)