Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách hiểu bins trong histogram Pyplot (Matplotlib)

Để hiểu cách bins trong histogram Pyplot được diễn giải với các loại bins khác nhau:

  • Thiết lập kích thước figure và padding
  • Tạo list dữ liệu cho histogram
  • Thêm subplot 1 hàng 3 cột index=1
  • Vẽ histogram bins là số (int)
  • Thêm subplot index=2
  • Vẽ histogram bins là mảng (array)
  • Thêm subplot index=3
  • Vẽ histogram bins là chuỗi (string - auto method)

Ví dụ mã nguồn

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

data = [1, 3, 1, 4, 7, 1, 3, 5, 4, 6, 5]
plt.subplot(131)
plt.hist(data, bins=len(data))
plt.subplot(132)
plt.hist(data, bins=[1, 3, 5, 7])
plt.subplot(133)
plt.hist(data, bins='stone')
plt.show()

Kết quả

Ba histogram cùng dữ liệu: (1) bins=int (11 bins), (2) bins=array [1,3,5,7] (3 bins tùy chỉnh), (3) bins='stone' (Stone estimator auto).

Cách hiểu bins trong histogram Pyplot (Matplotlib)

Các phương pháp bins string: 'auto', 'fd' (Freedman-Diaconis), 'doane', 'scott', 'stone', 'rice', 'sturges', 'sqrt'.