1. 导入依赖包
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
2. sns.histplot绘制直方图与偏分图
sns.histplot(x=train['SalePrice'], kde=True, color='blue',bins=50, stat='density')
x: 数据
kde: 用于设置是否显示核密度曲线
color: 图形的颜色
bins: 直方图呈现出的数据组数
stat: 直方图y轴显示的数据;'count':表示频数统计;frequency表示频数除以极差(全距);probability表示用小数点表示的频率;percent表示用百分数表示的频率;density表示概率密度,为frequency之和归一处理后的数据。
sns.histplot(x=train['SalePrice'], kde=True, color='blue',bins=50, stat='density')
plt.title('Histogram with Normal Distribution')
plt.xlabel('SalePrice')
plt.ylabel("Frequency")
3. plot绘制正态图
(mu,sigma) = norm.fit(train['SalePrice'])
x = np.linspace(0, 700000, 1000)
y = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - mu) / sigma) ** 2)
plt.plot(x, y,'r',linewidth=2)
4. 显示图像
plt.show()
5. 效果图