1 回答

TA贡献1895条经验 获得超3个赞
您的数据似乎是离散的,只允许整数值。因此,标准直方图可能会造成混淆,因为它会创建与离散值不对齐的大小相等的分箱。在这种情况下,许多箱子都是空的。(例如,当您有 100 个值时,箱子将获得多个值,但由于四舍五入,某些箱子将获得比其他箱子更多的值。)
应该给出明确的 bin,例如在整数之间的一半处有 bin 边界:
from matplotlib import pyplot as plt
import numpy as np
import seaborn as sns
p = np.random.rand(21) + 0.1
p /= p.sum()
raw_i = np.random.choice(range(21), size=1000000, p=p)
bins = np.arange( -0.5, raw_i.max()+1, 1)
fig, (ax_box, ax_hist) = plt.subplots(2, sharex=True, gridspec_kw={"height_ratios": (.15, .85)})
sns.distplot(raw_i, bins=bins, ax=ax_hist, kde=False)
sns.boxplot(raw_i, ax=ax_box)
ax_box.set(xlabel='')
sns.despine(ax=ax_hist)
sns.despine(ax=ax_box, left=True)
ax_box.set_yticks([])
plt.show()
添加回答
举报