Biểu đồ hexbin (hexagonal binning) hữu ích để trực quan hóa mật độ điểm dữ liệu lớn. Các bước thực hiện:
- Thiết lập figure size và autolayout.
- Tạo dữ liệu x, y phân phối chuẩn bằng NumPy.
- Tạo figure và subplots.
- Vẽ hexbin bằng phương thức hexbin() với tham số gridsize và cmap.
- Đặt tiêu đề cho biểu đồ.
- Hiển thị bằng show().
Ví dụ
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = 2 * np.random.randn(5000)
y = x + np.random.randn(5000)
fig, ax = plt.subplots()
_ = ax.hexbin(x[::10], y[::10], gridsize=20, cmap='plasma')
ax.set_title('Hexbin Histogram')
plt.show()
Kết quả
