如何绘制以下计数器对象的直方图?:
w = collections.Counter()
l = ['a', 'b', 'b', 'b', 'c']
for o in l:
w[o]+=1
如何绘制以下计数器对象的直方图?:
w = collections.Counter()
l = ['a', 'b', 'b', 'b', 'c']
for o in l:
w[o]+=1
TypeError: unhashable type: 'dict_keys'
错误。 - Leopoldoplt.bar(list(w.keys()), list(w.values()))
吗? - Sheldorelist
转换非常好,谢谢! - Leopoldowith plt.style.context('fivethirtyeight'):
- duhaimeimport matplotlib.pyplot as plt
import collections
l = ['a', 'b', 'b', 'b', 'c']
count = collections.Counter(l)
print(count)
plt.bar(range(len(count)), count.values())
plt.show()
Counter
的构造函数可以直接接受列表并给出计数,因此您的代码可以替换为collections.Counter(['a', 'b', 'b', 'b', 'c'])
。另外,您尝试过使用 matplotlib.pyplot.hist 吗? - MatsLindh