直方图不是您要查找的图形。使用条形图。
import numpy as np
import matplotlib.pyplot as plt
data = [1, 5, 1, 1, 6, 3, 3, 4, 5, 5, 5, 2, 5]
correlation = [(i, data.count(i)) for i in set(data)]
correlation.sort(key=lambda x: x[1])
labels, values = zip(*correlation)
indexes = np.arange(len(correlation))
width = 1
plt.bar(indexes, values, width)
plt.xticks(indexes + width * 0.5, labels)
plt.show()
编辑:
对于大数据集更好地使用
collections.Counter
而不是列表理解
count
.
这是一种更快地归档相同结果的方法(既没有条形图也没有历史记录):
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
data = np.random.random_integers(0, 10**4, 10**5)
correlation = Counter(data).items()
correlation.sort(key=lambda x: x[1])
labels, values = zip(*correlation)
indexes = np.arange(len(correlation))
plt.plot(indexes, values)
plt.fill_between(indexes, values, 0)
plt.show()