简体   繁体   中英

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.

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:

条形图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM