使用seaborn或matplotlib的分组箱图数据格式

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

我有3个数据框,其中所有3个数据框的列名和行数完全相同。我想使用seaborn或matplotlib将来自所有三个数据帧的所有列作为一个分组的箱线图绘制到一个图像中。但是我在合并和格式化数据时遇到困难,因此无法将它们绘制为成组的箱形图。

df =

          A         B         C         D  E  F  G         H         I  J
0  0.031810  0.000556  0.007798  0.000741  0  0  0  0.000180  0.002105  0
1  0.028687  0.000571  0.009356  0.000000  0  0  0  0.000183  0.001250  0
2  0.029635  0.001111  0.009121  0.000000  0  0  0  0.000194  0.001111  0
3  0.030579  0.002424  0.007672  0.000000  0  0  0  0.000194  0.001176  0
4  0.028544  0.002667  0.007973  0.000000  0  0  0  0.000179  0.001333  0
5  0.027286  0.003226  0.006881  0.000000  0  0  0  0.000196  0.001111  0
6  0.031597  0.003030  0.006695  0.000000  0  0  0  0.000180  0.002353  0
7  0.034226  0.003030  0.010804  0.000667  0  0  0  0.000179  0.003333  0
8  0.035105  0.002941  0.010176  0.000645  0  0  0  0.000364  0.003529  0
9  0.035171  0.003125  0.012666  0.001250  0  0  0  0.000612  0.005556  0 

df1 =

          A         B         C         D  E  F  G         H         I  J
0  0.034898  0.003750  0.014091  0.001290  0  0  0  0.001488  0.005333  0
1  0.042847  0.003243  0.011559  0.000625  0  0  0  0.002272  0.010769  0
2  0.046087  0.005455  0.013101  0.000588  0  0  0  0.002147  0.008750  0
3  0.042719  0.003684  0.010496  0.001333  0  0  0  0.002627  0.004444  0
4  0.042410  0.004211  0.011580  0.000645  0  0  0  0.003007  0.006250  0
5  0.044515  0.003500  0.013990  0.000000  0  0  0  0.003954  0.007000  0
6  0.046062  0.004865  0.013278  0.000714  0  0  0  0.004035  0.011111  0
7  0.043666  0.004444  0.013460  0.000625  0  0  0  0.003826  0.010000  0
8  0.039888  0.006857  0.014351  0.000690  0  0  0  0.004314  0.011474  0
9  0.048203  0.006667  0.016338  0.000741  0  0  0  0.005294  0.013603  0

df3 =

          A         B         C         D  E  F  G         H         I  J
0  0.048576  0.006471  0.020130  0.002667  0  0  0  0.005536  0.015179  0
1  0.056270  0.007179  0.021519  0.001429  0  0  0  0.005524  0.012333  0
2  0.054020  0.008235  0.024464  0.001538  0  0  0  0.005926  0.010445  0
3  0.047297  0.008649  0.026650  0.002198  0  0  0  0.005870  0.010000  0
4  0.049347  0.009412  0.022808  0.002838  0  0  0  0.006541  0.012222  0
5  0.052026  0.010000  0.019935  0.002714  0  0  0  0.005062  0.012222  0
6  0.055124  0.010625  0.022950  0.003499  0  0  0  0.005954  0.008964  0
7  0.044411  0.010909  0.019129  0.005709  0  0  0  0.005209  0.007222  0
8  0.047697  0.010270  0.017234  0.008800  0  0  0  0.004808  0.008355  0
9  0.048562  0.010857  0.020219  0.008504  0  0  0  0.005665  0.004862  0

我可以使用以下方法进行单个箱形图:

g = sns.boxplot(data=df, color = 'white', fliersize=1, linewidth=2, meanline = True, showmeans=True)

但是如何在一个图中获得全部三个似乎有点困难。我看到我需要重新排列整个数据并使用色相,以便从组合数据框中获取所有内容,但是我应该如何精确格式化数据却是一个问题。有帮助吗?

pandas matplotlib seaborn python-3.5
1个回答
1
投票
将所有三个数据帧的所有列作为一个分组的箱线图绘制到一个图像中。

您可以通过连接数据帧并传递sns.boxplot来一次完成hue的全部工作:

tmp = (pd.concat([d.assign(data=i)                       # assign adds the column `data` with values i
                    for i,d in enumerate([df,df1,df3])]  # enumerate gives you a generator of pairs (0,df), (1,df1), (2,df2)
                )
         .melt(id_vars='data')                           # melt basically turns `id_vars` columns into index, 
                                                         # and stacks other columns
      )

sns.boxplot(data=tmp, x='variable', hue='data', y='value')

输出:

enter image description here

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