绘制Python中字符串列表的直方图

时间:2016-07-27 19:56:08

标签: python matplotlib histogram

我有很长的ID列表(ID是字符串值。 我想绘制这个列表的直方图。 stackoverflow上的其他线程中有一些代码用于绘制直方图,但我想要的直方图应该看起来像这张图片(即最高值在左侧,当x轴增加时值逐渐减小。

这是用于绘制常规直方图的代码

import pandas
from collections import Counter
items=a long list of strings
letter_counts = Counter(items)
df = pandas.DataFrame.from_dict(letter_counts, orient='index')
df.plot(kind='bar')

this one

1 个答案:

答案 0 :(得分:0)

这些方面的内容如何...

from collections import Counter
import matplotlib.pyplot as plt
import numpy as np

counts = Counter(['a','a','a','c','a','a','c','b','b','d', 'd','d','d','d','b'])
common = counts.most_common()
labels = [item[0] for item in common]
number = [item[1] for item in common]
nbars = len(common)

plt.bar(np.arange(nbars), number, tick_label=labels)
plt.show()

most_common()电话是此脚本的主要创新。其余的很容易在matplotlib文档中找到(已在评论中链接)。