首页 > 解决方案 > 如何将计数器列表中的数据显示为图表?

问题描述

我有一个程序可以计算每个数字的出现并将其添加到我的计数器中。我用过收款柜台。打印我的计数器时,我收到存储的数据。

Counter({1856: 109, 1331: 4, 1351: 3, 1341: 2, 1376: 2, 1371: 2, 1356: 1, 1361: 1, 1396: 1})

我的程序以显示此数据结束,但我也想将其显示为图表。有任何想法吗?

此有效载荷是在以下起始偏移处发现的:

The Start Offset: 1856 was Discovered a Total of 109 Times!

The Start Offset: 1331 was Discovered a Total of 4 Times!

The Start Offset: 1351 was Discovered a Total of 3 Times!

The Start Offset: 1341 was Discovered a Total of 2 Times!

The Start Offset: 1376 was Discovered a Total of 2 Times!

The Start Offset: 1371 was Discovered a Total of 2 Times!

The Start Offset: 1356 was Discovered a Total of 1 Times!

The Start Offset: 1361 was Discovered a Total of 1 Times!

The Start Offset: 1396 was Discovered a Total of 1 Times!

标签: python

解决方案


您有多种选择,但一种流行的选择是matplotlib.

这是一个简单的条形图,使用matplotlib.pyplot

from collections import Counter
import matplotlib.pyplot as plt

c = Counter({1856: 109, 1331: 4, 1351: 3, 1341: 2, 1376: 2, 1371: 2, 1356: 1, 1361: 1, 1396: 1})
plt.bar([str(key)  for key in c], c.values())
plt.show()

推荐阅读