如何在python中绘制直方图

2024-07-03 07:56:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

合计项目={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})

我想绘制一个柱状图来显示列表中不同项目的分布。

我已经用谷歌工作表绘图了,但是很费时。

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()

Tags: 项目import绘图列表matplotlibcounter绘制plt
1条回答
网友
1楼 · 发布于 2024-07-03 07:56:03

下面是一个简单的例子 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()

相关问题 更多 >