目标
python 使用matplotlib统计数组x中每个数字出现的次数。
实现
1. 生成数组 [ 1, 2, 2, 3, 3, 3 ... ]
2. 绘制直方图,其中x轴为10个单位(bins=10),y轴为每个数字出现的次数。
import matplotlib.pyplot as plt
x = []
for i in range(0, 10):
for j in range(0, i + 1):
x.append(i + 1)
print(x)
plt.hist(x, bins=10)
plt.show()