如何在python中绘制直方图

时间:2018-10-29 16:38:28

标签: python python-2.7

我有一个项目列表(那里有重复的项目)。

Total_Item = {12,13,13,16,.... 10}

执行时

 import collections
  ..
  ..

  print collections.Counter(Total_Item)

我得到了以下输出

Counter({13: 17, 12: 12, 14: 9, 15: 5, 11: 2, 17: 2, 10: 1, 16: 1})

我想绘制一个直方图以显示此列表中不同项目的分布。

我已经使用Google表格进行了绘制,但这很耗时。

enter image description here

如何在python中绘制直方图?

根据评论中的建议和此链接matplotlib, 我可以打印直方图。

  import matplotlib.pyplot as plt
  import collections
  ..
  ..

  print collections.Counter(Total_Item)
  plt.figure()
  plt.hist(Total_Item)
  plt.show()

1 个答案:

答案 0 :(得分:1)

这是一个简单的例子 https://pythonspot.com/matplotlib-histogram/

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]
num_bins = 5
n, bins, patches = plt.hist(x, num_bins, facecolor='blue', alpha=0.5)
plt.show()