Để vẽ đồ thị đường từ dữ liệu histogram, ta dùng numpy histogram để tính histogram của tập dữ liệu.
Các bước
- Thêm subplot vào hình hiện tại, nrows=2, ncols=1 và index=1.
- Dùng numpy histogram lấy histogram của dữ liệu.
- Vẽ histogram bằng hist() với edgecolor=black.
- Tại index 2, dùng dữ liệu tính được để vẽ bằng plot().
- Hiển thị bằng 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 plt.subplot(211) data = np.array(np.random.rand(100)) y, binEdges = np.histogram(data, bins=100) plt.hist(data, bins=100, edgecolor='black') plt.subplot(212) bincenters = 0.5 * (binEdges[1:] + binEdges[:-1]) plt.plot(bincenters, y, '-', c='black') plt.show()
Kết quả
