Để vẽ Box plot đè lên Swarm plot trong Seaborn, chúng ta có thể thực hiện các bước sau:
- 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.
- Khởi tạo plotter, swarmplot.
- Để vẽ box plot, dùng phương thức boxplot().
- Để hiển thị figure, dùng phương thức show().
Ví dụ
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = pd.DataFrame({"Box1": np.arange(10), "Box2": np.arange(10)})
ax = sns.swarmplot(x="Box1", y="Box2", data=data, zorder=0)
sns.boxplot(x="Box1", y="Box2", data=data, showcaps=False, boxprops={'facecolor': 'None'}, showfliers=False, whiskerprops={'linewidth': 0}, ax=ax)
plt.show()
Kết quả

