简体   繁体   English

seaborn distplot 每个图上不同的条宽

[英]seaborn distplot different bar width on each figure

Sorry for giving an image however I think it is the best way to show my problem.很抱歉提供图片,但我认为这是展示我的问题的最佳方式。 在此处输入图像描述

As you can see all of the bin width are different, from my understanding it shows range of rent_hours.正如您所看到的,所有的 bin 宽度都不同,据我了解,它显示了rent_hours 的范围。 I am not sure why different figure have different bin width even though I didn't set any.我不确定为什么不同的图形有不同的 bin 宽度,即使我没有设置。

My code looks is as follows:我的代码如下所示:

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);

additional question:附加问题:

Seaborn: How to get the count in y axis for distplot using PairGrid for here it says that kde=False makes y-axis count however http://seaborn.pydata.org/generated/seaborn.distplot.html in the doc, it uses kde=False and still seems to show something else. Seaborn: How to get the count in y axis for distplot using PairGrid for here it says that kde=False makes y-axis count however http://seaborn.pydata.org/generated/seaborn.distplot.html in the doc, it使用 kde=False 并且似乎仍然显示其他内容。 How can I set y-axis to show count?如何设置 y 轴以显示计数?

I've tried sns.distplot(group.rent_hour, ax=ax, norm_hist=True) and it still seems to give something else rather than count.我试过 sns.distplot(group.rent_hour, ax=ax, norm_hist=True) ,它似乎仍然给出了其他东西而不是计数。

sns.distplot(group.rent_hour, ax=ax, kde=False) gives me count however I don't know why it is giving me count. sns.distplot(group.rent_hour, ax=ax, kde=False) 给我计数,但我不知道为什么它给我计数。

Answer 1:答案1:

From the documentation :文档中:

norm_hist : bool, optional norm_hist : bool,可选

If True, the histogram height shows a density rather than a count.如果为 True,则直方图高度显示密度而不是计数。 This is implied if a KDE or fitted density is plotted.如果绘制了 KDE 或拟合密度,则暗示这一点。

So you need to take into account your bin width as well, ie compute the area under the curve and not just the sum of the bin heights.因此,您还需要考虑您的 bin 宽度,即计算曲线下的面积,而不仅仅是 bin 高度的总和。

Answer 2:答案 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([])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM