python - Plotting histogram of list of tuplets matplotlib -


i have list of tuplets

k = [(8, 8),(10, 10),(8, 8),  (8, 8),(12, 12),(7, 7),(8, 8),  (9, 9),(10, 10),(10, 10),(8, 8),(9, 9),(13, 13),  (10, 10),(8, 8),(8, 8),(7, 7)] 

i want make simple histogram of frequency of each tuplet. how 1 go doing that?

standard plt.dist don't seem quite work, nor remapping tuplets single variables.

a histogram (numpy.hist, plt.hist) on continuous data, can separate in bins.

here want count identical tuples: can use collection.counter

from collections import counter k = [(8, 8),(10, 10),(8, 8),  (8, 8),(12, 12),(7, 7),(8, 8),  (9, 9),(10, 10),(10, 10),(8, 8),(9, 9),(13, 13),  (10, 10),(8, 8),(8, 8),(7, 7)]  c=counter(k) >>> counter({(8, 8): 7, (10, 10): 4, (9, 9): 2, (7, 7): 2, (13, 13): 1, (12, 12): 1}) 

after formatting, can use plt.bar plot count of each tuple, in histogram fashion.

# x axis: 1 point per key in counter (=unique tuple) x=range(len(c)) # y axis: count each tuple, sorted tuple value y=[c[key] key in sorted(c)] # labels x axis: tuple strings xlabels=[str(t) t in sorted(c)]  # plot plt.bar(x,y,width=1) # set labels @ middle of bars plt.xticks([x+0.5 x in x],xlabels) 

tuple bar plot


Comments