数组列表的小提琴图

时间:2020-09-14 12:22:03

标签: python pandas matplotlib violin-plot

我有一些数据,格式为:

[array([[0, 1, 2]], dtype=int64), array([[1, 2, 3]], dtype=int64)]

我的数据可以使用以下方式生成:

di_DFs = {}
groups = [1,2]
for grp in groups:
    di_DFs[grp] = pd.DataFrame({'A' : [grp-1],
                                'B' : [grp],
                                'C' : [grp+1]})
data = []
for k in di_DFs:
    data.append(di_DFs[k].iloc[[0]].values)

我可以绘制它:

for v in data:
    plt.scatter(range(len(v[0])),v[0])

enter image description here

我想得到一个带有3个垂直小提琴的小提琴图,请把我的成对点放在散点图中,以比较数组中的分布。我尝试过:

for v in data:
    plt.violinplot(v)

但是我得到了

enter image description here

2 个答案:

答案 0 :(得分:0)

我相信您需要这样的东西:

for v in data:
    plt.violinplot(v)

绘制此图:

enter image description here

由于示例数据集只有几个点,因此您不会看到太多的分布,而更像是平面破折号/点。但是尝试使用更多的数据点,它将满足需要。

答案 1 :(得分:0)

我需要重新格式化数据:

df_Vi = pd.DataFrame({'Z' : data[0][0],
                      'Y' : data[1][0]}, index=range(len(data[0][0])))
    
plt.violinplot(df_Vi)

enter image description here

或者,可以处理更多数据的版本:


di_DFs = {}
groups = [1,2,0,7]
for grp in groups:
    di_DFs[grp] = pd.DataFrame({'A' : [grp-1],
                                'B' : [grp],
                                'C' : [grp+1]})
data = []
for k in di_DFs:
    data.append(di_DFs[k].iloc[[0]].values)

Indexes = range(len(groups))
    
df_Vi = pd.DataFrame()
    
for inD in Indexes:
    df_Po = pd.DataFrame({inD : data[inD][0]},
                          index=range(len(data[0][0])))
    
    df_Vi = pd.concat([df_Vi, df_Po], axis=1)

plt.violinplot(df_Vi)

enter image description here