当输入为列表列表时,对海底波箱/小提琴进行分组

问题描述 投票:0回答:1

我尝试从列表中绘制多个分组的箱形图和小提琴图。取消分组后,这种近似对我有用:

boxes_sep = 0.4
list1 = np.array([0.78615544, 0.78416606, 0.78346039, 0.782058]) # and so on
ax = sns.boxplot(data=[list1, list2, list3], palette="Set2", width=boxes_sep)
ax1 = sns.violinplot(data=[list1, list2, list3], color=".22", width=boxes_sep)

plt.setp(ax1.collections, alpha=.25)
plt.xticks([0,1,2], ("A", "B", "C"))

获取下一张照片

enter image description here

现在,我想进行比较以绘制分组箱图和小提琴图,而我尝试了类似的操作:

ax = sns.boxplot(data=[[list1A,list1B] [list2A,list2B], [list3A,list3B]], width=boxes_sep)
ax1 = sns.violinplot(data=[[list1A,list1B], [list2A,list2B], [list3A,list3B]], width=boxes_sep)

我尝试按照https://stackoverflow.com/a/56498949/6724947之类的过去解决方案将其转换为数据帧,但没有成功。

python matplotlib seaborn
1个回答
0
投票
import pandas as pd
df = pd.DataFrame({"1A": list1A,
                   "1B": list1B,
                   "2A": list2A,
                   "2B": list2B,
                   "3A": list3A,
                   "3B": list3B})
df = df.melt()
df["no"] = df["variable"].apply(lambda x: x[0])
df["letter"] = df["variable"].apply(lambda x: x[1])
boxes_sep = 0.4
fig, ax = plt.subplots()
sns.violinplot(data=df, x="no", y="value", hue="letter", color=".22", width=boxes_sep, ax=ax)
sns.boxplot(data=df, x="no", y="value", hue="letter", palette="Set2", width=boxes_sep, ax=ax)
plt.setp(ax.collections, alpha=.25)

Is this what you're looking for?

© www.soinside.com 2019 - 2024. All rights reserved.