如何使用matplotlib绘制collections.Counter直方图? [英] How to plot collections.Counter histogram using matplotlib?
本文介绍了如何使用matplotlib绘制collections.Counter直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何绘制以下Counter对象的直方图?
How to plot histogram of following Counter object?:
w = collections.Counter()
l = ['a', 'b', 'b', 'b', 'c']
for o in l:
w[o]+=1
推荐答案
查看您的数据并进行尝试,我想您想要的是条形图而不是直方图.直方图用于绘制分布,但这不是您所拥有的.您可以简单地使用 keys
和 values
作为 plt.bar
的参数.这样,键将被自动用作x轴刻度标签.
Looking at your data and attempt, I guess you want a bar plot instead of a histogram. Histogram is used to plot a distribution but that is not what you have. You can simply use the keys
and values
as the arguments of plt.bar
. This way, the keys will be automatically taken as the x-axis tick-labels.
import collections
import matplotlib.pyplot as plt
l = ['a', 'b', 'b', 'b', 'c']
w = collections.Counter(l)
plt.bar(w.keys(), w.values())
这篇关于如何使用matplotlib绘制collections.Counter直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文