Tính bins chung từ np.histogram(np.hstack((a,b)), bins=10) rồi dùng cho cả hai hist.
- a = random*0.5, b = 1 - normal*0.1.
- bins = 10 → bins = np.histogram(np.hstack((a,b)), bins=bins)[1].
- plt.hist(a, bins, edgecolor='black'), plt.hist(b, bins, edgecolor='black').
- show().
Ví dụ
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True a = np.random.random(100) * 0.5 b = 1 - np.random.normal(size=100) * 0.1 bins = 10 bins = np.histogram(np.hstack((a, b)), bins=bins)[1] plt.hist(a, bins, edgecolor='black') plt.hist(b, bins, edgecolor='black') plt.show()
Kết quả
Hai histogram chồng lên nhau, cùng 10 bins, đường viền đen, dễ so sánh phân phối.