How to make a histogram from a list of strings in Python?

前端 未结 8 1231
说谎
说谎 2020-12-03 04:29

I have a list of strings:

a = [\'a\', \'a\', \'a\', \'a\', \'b\', \'b\', \'c\', \'c\', \'c\', \'d\', \'e\', \'e\', \'e\', \'e\', \'e\']

I w

8条回答
  •  北海茫月
    2020-12-03 05:07

    Simple and effective way to make character histrogram in python

    import numpy as np
    
    import matplotlib.pyplot as plt
    
    from collections import Counter
    
    
    
    a = []
    count =0
    d = dict()
    filename = raw_input("Enter file name: ")
    with open(filename,'r') as f:
        for word in f:
            for letter  in word:
                if letter not in d:
                    d[letter] = 1
                else:
                    d[letter] +=1
    num = Counter(d)
    x = list(num.values())
    y = list(num.keys())
    
    x_coordinates = np.arange(len(num.keys()))
    plt.bar(x_coordinates,x)
    plt.xticks(x_coordinates,y)
    plt.show()
    print x,y

提交回复
热议问题