我尝试从列表中绘制多个分组的箱形图和小提琴图。取消分组后,这种近似对我有用:
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"))
获取下一张图片
现在,我想进行比较,以绘制成组的箱线图和小提琴图,我尝试了类似的操作:
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之类的过去解决方案将其转换为数据帧,但没有成功。
答案 0 :(得分:2)
import pandas as pd
import numpy as np
list1A = np.random.randn(50)
list1B = np.random.randn(50)
list2A = np.random.randn(50)
list2B = np.random.randn(50)
list3A = np.random.randn(50)
list3B = np.random.randn(50)
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)