Để điều chỉnh độ rộng box trong boxplot Python matplotlib, chúng ta có thể dùng tham số widths trong phương thức boxplot().
Các bước
- Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
- Tạo Pandas dataframe, dữ liệu bảng hai chiều, kích thước có thể thay đổi, có thể hétérogène.
- Vẽ box and whisker plot, dùng boxplot() với tuple widths để điều chỉnh box.
- Để hiển thị figure, dùng phương thức show().
Ví dụ
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = pd.DataFrame({"Box1": np.random.rand(10), "Box2": np.random.rand(10)})
ax = plt.boxplot(data, widths=(0.25, 0.5))
plt.show()
Kết quả

