ในการทำ histogram bins พื้นที่เท่ากัน (equal area) ใน matplotlib สามารถทำตามขั้นตอนดังนี้:
ขั้นตอน
ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
สร้างข้อมูลสุ่มด้วย numpy
วาด histogram ด้วยเมธอด equal_area ทำให้พื้นที่ patches เท่ากัน
แสดงรูปด้วยเมธอด show()
ตัวอย่าง
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
def equal_area(x, nbin):
pow = 0.5
dx = np.diff(np.sort(x))
tmp = np.cumsum(dx ** pow)
tmp = np.pad(tmp, (1, 0), 'constant')
return np.interp(np.linspace(0, tmp.max(), nbin + 1), tmp, np.sort(x))
x = np.random.randn(1000)
n, bins, patches = plt.hist(x, equal_area(x, 20), edgecolor='black')
plt.show()
ผลลัพธ์
จะแสดง histogram 20 bins พื้นที่เท่ากัน ไม่ใช่ความกว้างเท่ากัน
