Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create histogram on values not on counts using Python?

I want to create a histogram using Python and Matplotlib from the values in an array not on the count of the values in the array. For example:-

X = [0,0,0,1,10,5,0,0,5]

If I use the below code

n, bins, patches = plt.hist(X) plt.show()

I get this histogram

This is counting the number of occurrences and creating the histogram. The output should be like this:- Expected Plot

like image 848
naveen Avatar asked Sep 13 '25 04:09

naveen


1 Answers

Looks like you want a bar graph rather than a histogram. Note that a histogram is:

An accurate representation of the distribution of numerical data

It differs from a bar graph, in the sense that a bar graph relates two variables, but a histogram relates only one. For plotting a bar graph you could use matplotlib.pyplot.bar:

X = [0,0,0,1,10,5,0,0,5]

import matplotlib.pyplot as plt 
plt.bar(range(len(X)), X)

enter image description here

like image 154
yatu Avatar answered Sep 15 '25 17:09

yatu