python plot simple histogram given binned data

86,435

Solution 1

If I'm understanding what you want to achieve correctly then the following should give you what you want:

import matplotlib.pyplot as plt
plt.bar(range(0,100), x)
plt.show()

It doesn't use hist(), but it looks like you've already put your data into bins so there's no need.

Solution 2

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.

Solution 3

Cool, thanks! Here's what I think the OP wanted to do:

import random
import matplotlib.pyplot as plt
x=[x/1000 for x in random.sample(range(100000),100)]
xbins=range(0,len(x))
plt.hist(x, bins=xbins, color='blue')
plt.show()

Solution 4

I am fairly sure that your problem is the bins. It is not a list of limits but rather a list of bin edges.

xbins = [0,len(x)]

returns in your case a list containing [0, 100] Indicating that you want a bin edge at 0 and one at 100. So you get one bin from 0 to 100. What you want is:

xbins = [x for x in range(len(x))]

Which returns:

[0,1,2,3, ... 99]

Which indicates the bin edges you want.

Solution 5

You can achieve this using matplotlib's hist as well, no need for numpy. You have essentially already created the bins as xbins. In this case x will be your weights.

plt.hist(xbins,weights=x)
Share:
86,435
Curious
Author by

Curious

Updated on June 28, 2020

Comments

  • Curious
    Curious almost 4 years

    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 correctly because my data is already binned.

    import random
    import matplotlib.pyplot as plt
    x = random.sample(range(1000), 100)
    xbins = [0, len(x)]
    #plt.hist(x, bins=xbins, color = 'blue') 
    #Does not make the histogram correct. It counts the occurances of the individual counts. 
    
    plt.plot(x)
    #plot works but I need this in histogram format
    plt.show()