Python如何用不同长度的向量的箱形图创建一个图

时间:2018-12-12 15:45:43

标签: python graph visualization seaborn boxplot

我有两个长度不同的列表:

l1 = [1,5,6,7,8,9,4,5,6,8,9,4,5,6,7,1,2,3,8,9,5,9,8,7,3,5,8,6]
l2 = [0,1,2,3,4,6,1,2,8,2,3,1,3,4,4,1,2,2,3,9,1,2,0,0,0,1]

我想用盒/小提琴图创建一个图,这样它们将彼此相邻。 即使长度不同我怎么办?

1 个答案:

答案 0 :(得分:0)

对于您的特定示例,它应该与绘制具有相等长度向量的图一样简单:

import matplotlib.pyplot as plt
import seaborn as sns

l1 = [1,5,6,7,8,9,4,5,6,8,9,4,5,6,7,1,2,3,8,9,5,9,8,7,3,5,8,6]
l2 = [0,1,2,3,4,6,1,2,8,2,3,1,3,4,4,1,2,2,3,9,1,2,0,0,0,1]

fig, ((mpl_box_ax, sns_box_ax), (mpl_violin_ax, sns_violin_ax)) = plt.subplots(2, 2)

mpl_box_ax.boxplot([l1, l2])
mpl_violin_ax.violinplot([l1, l2])

sns.boxplot(data=[l1, l2], ax=sns_box_ax)
sns.violinplot(data=[l1, l2], ax=sns_violin_ax)

plt.show()

enter image description here