python plot simple histogram given binned data

前端 未结 6 1411
生来不讨喜
生来不讨喜 2020-12-03 00:58

I have count data (a 100 of them), each correspond to a bin (0 to 99). I need to plot these data as histogram. However, histogram count those data and does not plot correctl

6条回答
  •  星月不相逢
    2020-12-03 01:52

    The problem is with your xbins. You currently have

    xbins = [0, len(x)]
    

    which will give you the list [0, 100]. This means you will only see 1 bin (not 2) bounded below by 0 and above by 100. I am not totally sure what you want from your histogram. If you want to have 2 unevenly spaced bins, you can use

    xbins = [0, 100, 1000]
    

    to show everything below 100 in one bin, and everything else in the other bin. Another option would be to use an integer value to get a certain number of evenly spaced bins. In other words do

    plt.hist(x, bins=50, color='blue')
    

    where bins is the number of desired bins.

    On a side note, whenever I can't remember how to do something with matplotlib, I will usually just go to the thumbnail gallery and find an example that looks more or less what I am trying to accomplish. These examples all have accompanying source code so they are quite helpful. The documentation for matplotlib can also be very handy.

提交回复
热议问题