在Seaborn中串联多个Barplot

时间:2018-09-12 21:27:00

标签: python-3.x bar-chart seaborn

我的数据框包含以下列标题:主题,组,MASQ_GDA,MASQ_AA,MASQ_GDD,MASQ_AD

我成功地使用了具有以下规格的条形图来绘制其中之一:

bar_plot = sns.barplot(x="Group", y='MASQ_GDA', units="subject", ci = 68, hue="Group", data=demo_masq)

但是,我试图并排创建多个这样的条形图。可能有人知道我该怎么做,因为每个图都包含其余3个变量(MASQ_AA,MASQ_GDD,MASQ_AD)。这是我要实现的目标的一个示例。enter image description here

1 个答案:

答案 0 :(得分:1)

如果您查看the documentation for sns.barplot(),您会看到该函数接受参数+----------+-------+-------+-------+-------+ |column1_ID|column2|column3|column4|lastTwo| +----------+-------+-------+-------+-------+ |A_123 |12 |A |1 |[A,1] | |A_123 |12 |B |2 |[B,2] | |A_123 |23 |A |1 |[A,1] | |B_456 |56 |DB |4 |[DB,4] | |B_456 |56 |BD |5 |[BD,5] | |B_456 |60 |BD |3 |[BD,3] | +----------+-------+-------+-------+-------+ +----------+-------+----------------+ |column1_ID|column2|lastTwoArray | +----------+-------+----------------+ |B_456 |60 |[[BD,3]] | |A_123 |12 |[[A,1], [B,2]] | |B_456 |56 |[[DB,4], [BD,5]]| |A_123 |23 |[[A,1]] | +----------+-------+----------------+ +----------+-------+----------------+---------------------------------+ |column1_ID|column2|lastTwoArray |lastThree | +----------+-------+----------------+---------------------------------+ |B_456 |60 |[[BD,3]] |[60,WrappedArray([BD,3])] | |A_123 |12 |[[A,1], [B,2]] |[12,WrappedArray([A,1], [B,2])] | |B_456 |56 |[[DB,4], [BD,5]]|[56,WrappedArray([DB,4], [BD,5])]| |A_123 |23 |[[A,1]] |[23,WrappedArray([A,1])] | +----------+-------+----------------+---------------------------------+ root |-- column1_ID: string (nullable = true) |-- lastThreeArray: array (nullable = true) | |-- element: struct (containsNull = true) | | |-- column2: integer (nullable = false) | | |-- lastTwoArray: array (nullable = true) | | | |-- element: struct (containsNull = true) | | | | |-- column3: string (nullable = true) | | | | |-- column4: integer (nullable = false) +----------+--------------------------------------------------------------+ |column1_ID|lastThreeArray | +----------+--------------------------------------------------------------+ |B_456 |[[60,WrappedArray([BD,3])], [56,WrappedArray([DB,4], [BD,5])]]| |A_123 |[[12,WrappedArray([A,1], [B,2])], [23,WrappedArray([A,1])]] | +----------+--------------------------------------------------------------+ +----------+----------------------------------------------------+ |column1_ID|UDF(lastThreeArray) | +----------+----------------------------------------------------+ |B_456 |Map(60 -> Map(BD -> 3), 56 -> Map(DB -> 4, BD -> 5))| |A_123 |Map(12 -> Map(A -> 1, B -> 2), 23 -> Map(A -> 1)) | +----------+----------------------------------------------------+ ,使您可以告诉seaborn使用哪个轴对象来绘制结果

  

ax:matplotlib轴,可选

     

将绘图绘制到的轴对象,否则使用当前轴。

因此,获得所需输出的简单方法是预先创建轴,然后使用相应的ax=参数调用sns.barplot()

ax

另一种选择,也许是一种更符合seaborn哲学的选择,是使用a FacetGrid。这将允许您根据数据集中的类别数量自动创建所需的子图数量。但是,它需要调整数据框的形状,以使fig, axs = plt.subplots(1,4) # create 4 subplots on 1 row for ax,col in zip(axs,["MASQ_GDA", "MASQ_AA", "MASQ_GDD", "MASQ_AD"]): sns.barplot(x="Group", y=col, units="subject", ci = 68, hue="Group", data=demo_masq, ax=ax) # <- notice ax= argument 列的内容位于单个列上,并在新列中显示每个值对应的类别。