分布直方图中的加权区间

时间:2016-12-20 22:13:03

标签: python matplotlib histogram seaborn

我正在寻找一种绘制分布直方图的方法,y-axis代表每个分档的项目总数(而不仅仅是数量)。

以下图表中的示例:

  • 在左边,有55家代理商在20-30间房屋之间出售
  • 在右边,销售了20至30间房屋的代理商代表了1100套房屋

enter image description here

它并不像它看起来那么简单,因为人们不能简单地将每个bin的数量乘以bin的值(可能在20-30 bin中,有54个售出21的代理商是1人,售出29)。

问题:

  • 这样的图表的名称是什么(右边的那个)?
  • 有没有办法在matplotlibseaborn
  • 中原生地绘制它

2 个答案:

答案 0 :(得分:5)

您想使用通过weightnumpy docs)传递的ax.hist kwarg(参见see)。

这样的东西
fig, ax = plt.subplots()
ax.hist(num_sold, bins, weights=num_sold)

答案 1 :(得分:2)

编辑:@tacaswell更好地使用它。但我的标签将正确排列,没有麻烦,条形将分开。

希望你的数据在大熊猫中。我将创建一些假数据,然后给你一个解决方案。

import pandas as pd

# create a dataframe of number of homes sold
df = pd.DataFrame(data={'sold':np.random.randint(0,100, 1000)})

# groupby the left side of interval [0, 10), [10, 20) etc..  and plot
df.groupby(df.sold // 10 * 10).sum().plot.bar()