【问题标题】:Weighted bins in a distribution hist plot分布直方图中的加权 bin
【发布时间】:2017-05-06 05:53:26
【问题描述】:

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

下图示例:

  • 左边是55家中介,卖了20-30间房子
  • 右侧,售出20-30间房屋的中介代表已售出1100间房屋

这并不像看起来那么简单,因为不能简单地将每个 bin 的计数乘以 bin 的值(也许在 20-30 的 bin 中,有 54 家代理商卖出了 21 是 1 卖出了 29)。

问题:

  • 这样的图表叫什么名字(右边那个)?
  • 有没有办法在 matplotlibseaborn 中本地绘制它?

【问题讨论】:

    标签: python matplotlib histogram seaborn


    【解决方案1】:

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

    类似

    fig, ax = plt.subplots()
    ax.hist(num_sold, bins, weights=num_sold)
    

    【讨论】:

    • 太棒了,谢谢。那么,这个图表的名称是...加权直方图吗?
    【解决方案2】:

    编辑:@tacaswell 最好使用它。但是我的标签会毫无困难地正确排列,并且条形会分开。

    希望您的数据在 pandas 中。我会创建一些假数据,然后给你一个解决方案。

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

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 2019-09-26
      • 1970-01-01
      • 2017-08-22
      • 2021-09-03
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多