Skip to content
Advertisement

Histogram from data which is already binned, I have bins and frequency values

All the matplotlib examples with hist() generate a data set, provide the data set to the hist function with some bins (possibly non-uniformly spaced) and the function automatically calculates and then plots the histogram.

I already have histogram data and I simply want to plot it, how can I do that?! For example, I have the bins (half open ranges are denoted by the square and curved bracket notation),

[0, 1)   0
[1, 2)   3
[2, 3)   8
[3, 4)   6
[4, 5)   2
[5, 6)   3
[6, 7)   1
[7, 8)   0

Advertisement

Answer

Perhaps the weight parameter would be of help in your problem.

import matplotlib.pyplot as plt

a= [1,2,3,4,5,6,7,8,9]
b= [5,3,4,5,3,2,1,2,3]
plt.hist(a,9, weights=b)
plt.show()

Or, as tcaswell said, you could just make a bar plot and change the x-axis.

Using matplotlib how could I plot a histogram with given data in python

Is a link.

Advertisement