首页 > 解决方案 > seaborn distplot 每个图上不同的条形宽度

问题描述

很抱歉提供图片,但我认为这是展示我的问题的最佳方式。在此处输入图像描述

正如您所看到的,所有的 bin 宽度都不同,据我了解,它显示了rent_hours 的范围。我不确定为什么不同的图形有不同的 bin 宽度,即使我没有设置。

我的代码如下所示:

figure, axes = plt.subplots(nrows=4, ncols=3)
figure.set_size_inches(18,14)
plt.subplots_adjust(hspace=0.5)

for ax, age_g in zip(axes.ravel(), age_cat):
    group = total_usage_df.loc[(total_usage_df.age_group == age_g) & (total_usage_df.day_of_week <= 4)]
    sns.distplot(group.rent_hour, ax=ax, kde=False)
    ax.set(title=age_g)
    ax.set_xlim([0, 24])

figure.suptitle("Weekday usage pattern", size=25);

附加问题:

Seaborn : How to get the count in y axis for distplot using PairGrid for here it says kde=False 使 y-axis count 然而http://seaborn.pydata.org/generated/seaborn.distplot.html在文档中,它使用 kde=False 并且似乎仍然显示其他内容。如何设置 y 轴以显示计数?

我试过 sns.distplot(group.rent_hour, ax=ax, norm_hist=True) ,它似乎仍然给出了其他东西而不是计数。

sns.distplot(group.rent_hour, ax=ax, kde=False) 给我计数,但我不知道为什么它给我计数。

标签: pythonmatplotlibseaborn

解决方案


答案1:

文档中:

norm_hist : bool,可选

如果为 True,则直方图高度显示密度而不是计数。如果绘制了 KDE 或拟合密度,则暗示这一点。

因此,您还需要考虑您的 bin 宽度,即计算曲线下的面积,而不仅仅是 bin 高度的总和。

答案 2:

# Plotting hist without kde
ax = sns.distplot(your_data, kde=False)

# Creating another Y axis
second_ax = ax.twinx()

#Plotting kde without hist on the second Y axis
sns.distplot(your_data, ax=second_ax, kde=True, hist=False)

#Removing Y ticks from the second axis
second_ax.set_yticks([])

推荐阅读