Manish
Manish

Reputation: 71

Histogram in Python

I have a list of numbers.The list is like [0,0,1,0,1 .... ] .Presently it has binary digits only but later on it can have decimal digits as well. I want to plot a histogram of this sequence in the list. When I use standard hist funcion of matplotlib library , I get only two bars.It counts all zeros and all ones and shows me the histogram with two bars. But I want to plot in a different way. I want a no of bars = length of list and Height of each bar = value in the list at ( position = bar# ).

Here is the code:

 def plot_histogram(self,li_input,):
     binseq = numpy.arange(len(li_input))
     tupl = matplotlib.pyplot.hist(li_input,bins=binseq)
     matplotlib.pyplot.show()

li_input is the list discussed above.

I can do it in a nasty way like :

li_input_mod = []
   for x in range(len(li_input)):
   li_input_mod += [x]*li_input[x]

and then plot it but i want something better.

Upvotes: 0

Views: 1026

Answers (1)

Maehler
Maehler

Reputation: 6331

The behavior you describe is the way a histogram works; it shows you the distribution of values. It sounds to me like you want to create a bar chart:

import matplotlib.pyplot as plt

x = [0,0,1,0,1,1,0,1,1,0,0,0,1]
plt.bar(range(len(x)), x, align='center')

which would produce:

Bar chart

Upvotes: 3

Related Questions