python plot simple histogram given binned data

Question:

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()
Asked By: Curious

||

Answers:

Have a look at the histogram examples in the matplotlib documentation. You should use the hist function. If it by default does not yield the result you expect, then play around with the arguments to hist and prepare/transform/modify your data before providing it to hist. It is not really clear to me what you want to achieve, so I cannot help at this point.

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.

Answered By: jlund3

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.

Answered By: Aaron S

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.

Answered By: redrah

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()
Answered By: Russ

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)
Answered By: csaladenes
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.