在 histplot 中将数据拆分为 bin

Splitting data into bins in histplot

我对 sns.histplot() 有疑问。据我了解,bins 参数应指示图中应包含多少个 bin。这里有一些代码可以形象化奇怪的(至少对我来说)行为。

    d = {'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10 , 11 , 12, 13, 14, 15], 'col2': [1, 1, 1, 1, 1, 1, 2, 2, 2 , 2 , 2, 2, 2, 2, 2]}
    df = pd.DataFrame(data=d)
    sns.histplot(data=df, x='col1', multiple='dodge', hue='col2', binwidth=2, bins=8)

我的原始代码中有几乎相同的问题:

hist = sns.histplot(data=Data, x=y_data, multiple='dodge', ax=axes[0], hue=i[2][1], binwidth=2, bins=10)

如您所见,只有一个 bin 数据有其最小值和 std,但它没有拆分为我声明的 bin 数量。为什么这不将数据拆分为提供的 bin 数量?如何更改代码以确保垃圾箱数量不变?

我认为问题出在 binwidth 参数上。也许只是尝试删除该参数,或将其设置为较小的值(0.2 或 0.1)。

来自docs,关于binwidth参数:

Width of each bin, overrides bins but can be used with binrange.

所以你不能同时指定binsbinwidth