1. 程式人生 > >視覺化庫-Matplotlib-直方圖(第四天)

視覺化庫-Matplotlib-直方圖(第四天)

1.plt.hist(array, bins, color)  # array表示數值, bins表示的是bin的範圍

data = np.random.normal(0, 20, 1000)
# 畫出bins值
bins = np.arange(-100, 100, 5)
plt.hist(data, bins, color='b')
# 進行x軸範圍的設定
plt.xlim([data.min()-5, data.max()+5])

2. 將兩個直方圖放在一張圖上

import random

# random.guass一種快速生成高斯數的方法
data1 = [random.gauss(15, 10) for
i in range(500)] data2 = [random.gauss(5, 5) for i in range(500)] bins = np.arange(-50, 50, 2.5) plt.hist(data1, bins, alpha=0.3, label='class1') plt.hist(data2, bins, alpha=0.3, label='class2') plt.legend() plt.show()