Python: 直方图绘制

目标

 

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章