统计各个数字出现的次数及pyplot画图

2022-08-01,,,,

import matplotlib.pyplot as plt
import random
import pandas as pd
import collections

# 绘制条形图
array = []
# 随机数
for i in range(10):
    array.append(random.randint(0, 50))
# 统计各个元素出现的次数
b = collections.Counter(array)
# 转换成字典的格式
dic = {number: value for number, value in b.items()}
plt.title = "统计数字出现的次数"
# 取得key
x = [i for i in dic.keys()]
y = []
# 取得value
for i in dic.keys():
    y.append(dic.get(i))
# 转换成dataFrame的格式
df = pd.DataFrame(y, x)
# plt.hist(y, align='mid')
# 一个图表中画多个图
plt.xlabel = "number"
plt.ylabel = "count"
plt.subplot(221)
# 画折线图
plt.plot(x, y)
plt.subplot(222)
#画直方图
plt.hist(x)
plt.subplot(223)
# 以dataFrame的格式画图
df.plot()
plt.show()

本文地址:https://blog.csdn.net/weixin_38828673/article/details/107574786

《统计各个数字出现的次数及pyplot画图.doc》

下载本文的Word格式文档,以方便收藏与打印。