【问题标题】:Python: Plot histograms with customized binsPython:使用自定义的 bins 绘制直方图
【发布时间】:2023-01-11 03:43:07
【问题描述】:

我正在使用 matplotlib.pyplot 制作直方图。由于数据的分布,我想手动设置垃圾箱。详情如下所示:

  1. 一个bin中的任意值= 0;
  2. 最后一个bin中的任意值> 60;
  3. 任何值> 0和<= 60都在上述bin之间,bin大小为5。

    你能帮我一些忙吗?谢谢你。

【问题讨论】:

    标签: python-3.x matplotlib histogram


    【解决方案1】:

    我不确定“垃圾箱大小为 5”是什么意思。您可以通过 specifying the bins with a sequence 绘制直方图:

    import matplotlib.pyplot as plt
    data = [0, 0, 1, 2, 3, 4, 5, 6, 35, 60, 61, 82, -5] # your data here
    plt.hist(data, bins=[0, 0.5, 60, max(data)])
    plt.show()
    

    但是 bin 大小将匹配相应的间隔,这意味着 - 在这个例子中 - “0-case”将几乎不可见:

    (请注意,当将 bins 指定为序列时,60 将移至最后一个 bin,将序列更改为 [0, 0.5, 59.5, max(data)] 将解决此问题)

    您(可能)需要的是首先对数据进行分类,然后绘制类别的条形图:

    import matplotlib.pyplot as plt
    import pandas as pd
    
    data = [0, 0, 1, 2, 3, 4, 5, 6, 35, 60, 61, 82, -5] # your data here
    
    df = pd.DataFrame()
    df['data'] = data
    
    def find_cat(x):
        if x == 0:
            return "0"
        elif x > 60:
            return "> 60"
        elif x > 0:
            return "> 0 and <= 60"
    
    df['category'] = df['data'].apply(find_cat)
    df.groupby('category', as_index=False).count().plot.bar(x='category', y='data', rot=0, width=0.8)
    plt.show()
    

    输出:

    【讨论】:

    • 谢谢你的解决方案。对于&gt; 0 and &lt;= 60 category,我们是否可以进一步拆分为间隔宽度为5的间隔?如果我的问题不清楚,请告诉我。谢谢你。
    【解决方案2】:

    根据 Tranbi 的答案,您可以按照他们共享的链接中的详细信息指定 bin 边缘。

    import matplotlib.pyplot as plt
    import pandas as pd
    data = [0, 0, 1, 2, 3, 4, 5, 6, 35, 60, 61, 82, -6] # your data here
    df = pd.DataFrame()
    df['data'] = data
    
    bin_edges = [-5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65]
    bin_edges_offset = [x+0.000001 for x in bin_edges]
    
    plt.figure()
    plt.hist(df['data'], bins=bin_edges_offset)
    plt.show()
    

    histogram

    【讨论】:

    • 嗨罗伯特,谢谢你的解决方案。
    【解决方案3】:

    IIUC 你想要一个经典的直方图,值在 0(不包括)和 60(包括)之间,并在侧面为 0 和 >60 添加两个 bin。

    在那种情况下,我建议分别绘制 3 个区域:

    import matplotlib.pyplot as plt
    import pandas as pd
    
    data = [0, 0, 1, 2, 3, 4, 5, 6, 35, 60, 61, 82, -3] # your data here
    
    fig, axes = plt.subplots(1,3, sharey=True, width_ratios=[1, 12, 1])
    fig.subplots_adjust(wspace=0)
    
    # counting 0 values and drawing a bar between -5 and 0
    axes[0].bar(-5, data.count(0), width=5, align='edge')   
    axes[0].xaxis.set_visible(False)
    axes[0].spines['right'].set_visible(False)
    axes[0].set_xlim((-5, 0))
    
    # histogramm between (0, 60]
    axes[1].hist(data, bins=12, range=(0.0001, 60.0001))
    axes[1].yaxis.set_visible(False)
    axes[1].spines['left'].set_visible(False)
    axes[1].spines['right'].set_visible(False)
    axes[1].set_xlim((0, 60))
    
    # counting values > 60 and drawing a bar between 60 and 65
    axes[2].bar(60, len([x for x in data if x > 60]), width=5, align='edge')
    axes[2].xaxis.set_visible(False)
    axes[2].yaxis.set_visible(False)
    axes[2].spines['left'].set_visible(False)
    axes[2].set_xlim((60, 65))
    
    plt.show()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      相关资源
      最近更新 更多