首页 > 解决方案 > 如何为 txt 文件中的给定数据分配适当的 bin 值以绘制直方图

问题描述

我的文本文件中有一组数据。我不确定我应该如何考虑我的 bin 值以便在 python 中使用 matplotlib 绘制直方图。此外,我得到了一些奇怪的量级之王。

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib
    f= np.loadtxt('load.txt', delimiter=',', unpack= True)
    # set bins' interval for your data
    # You have following intervals: 
    # 1st col is number of data elements in [0,10000);
    # 2nd col is number of data elements in [10000, 20000); 
    # ...
    # last col is number of data elements in [100000, 200000]; 
    #bins = [0,10000,20000,50000,100000,200000] 
    bins = [0,50,100,150,200]
    plt.hist(f, histtype = 'bar', bins = bins)
    plt.xlabel('x values')
    plt.ylabel('y values')
    plt.title('Random plot')
    #plt.legend()
    plt.show()

    data:
    1,100
    2,10
    3,50
    4,110
    5,120
    6,200

标签: pythonhistogram

解决方案


推荐阅读