2017-07-25 107 views
-2

我需要使用FaceGrid boxplot來繪製以下數據框,以便網格的每一行對應於特定的一週,由k_week給出,列是數據幀的列(SensitivitySpecificity等)。每個箱線圖應包含6種型號( '西格', 'MRM', '平均',等等)嵌套熊貓數據幀的FaceGrid

這裏是單行的例子(我成功地得到它):

enter image description here

現在我需要使用FaceGrid在單個圖上獲取這6行。

數據框:

df.groupby('k_week').head(6) 
    Sensitivity Specificity Accuracy  PPV  AUC k_week model 
0  0.609948  0.867516 0.717291 0.776004 0.813680  4  Sig 
1  0.586824  0.851647 0.685525 0.757557 0.784805  4  MRM 
2  0.581152  0.842510 0.670357 0.749651 0.737269  4  Mean 
3  0.473386  0.601827 0.395841 0.556193 0.542679  4 Rmssd 
4  0.511344  0.717240 0.499148 0.644086 0.626719  4 MissVal 
5  0.454625  0.523924 0.356061 0.508448 0.501661  4  Rand 
0  0.792619  0.764423 0.755839 0.777934 0.835871  6  Sig 
1  0.722077  0.758113 0.733090 0.740845 0.803949  6  MRM 
2  0.633899  0.735877 0.688298 0.687011 0.736115  6  Mean 
3  0.437296  0.613582 0.510095 0.529108 0.544014  6 Rmssd 
4  0.517309  0.740986 0.647588 0.633803 0.653312  6 MissVal 
5  0.439582  0.532452 0.477898 0.502504 0.499943  6  Rand 
0  0.716681  0.778103 0.762680 0.747469 0.826501  8  Sig 
1  0.675493  0.772834 0.747396 0.724285 0.796723  8  MRM 
2  0.633127  0.743852 0.710935 0.688628 0.732537  8  Mean 
3  0.430421  0.685597 0.576665 0.558327 0.588858  8 Rmssd 
4  0.577817  0.671838 0.636629 0.624945 0.652066  8 MissVal 
5  0.470138  0.520199 0.494885 0.497872 0.505699  8  Rand 
0  0.744211  0.823977 0.778372 0.787783 0.849315  12  Sig 
1  0.672321  0.826884 0.763375 0.756750 0.812551  12  MRM 
2  0.623856  0.700291 0.633579 0.665608 0.719829  12  Mean 
3  0.405223  0.711921 0.538847 0.572755 0.581289  12 Rmssd 
4  0.592623  0.751062 0.664152 0.679169 0.709493  12 MissVal 
5  0.543888  0.463655 0.450788 0.496762 0.490970  12  Rand 
0  0.775064  0.794737 0.821246 0.783939 0.855578  20  Sig 
1  0.734351  0.745820 0.778527 0.739525 0.809667  20  MRM 
2  0.571756  0.759133 0.742810 0.656285 0.724444  20  Mean 
3  0.649109  0.433437 0.582287 0.551816 0.568039  20 Rmssd 
4  0.540712  0.820743 0.785873 0.667039 0.741546  20 MissVal 
5  0.550127  0.448916 0.546867 0.506564 0.498109  20  Rand 
0  0.813607  0.824252 0.891650 0.817440 0.889695  50  Sig 
1  0.671687  0.789921 0.850381 0.714253 0.826069  50  MRM 
2  0.677002  0.769764 0.839411 0.710398 0.772619  50  Mean 
3  0.410524  0.658583 0.681270 0.499830 0.563487  50 Rmssd 
4  0.630758  0.773543 0.831970 0.682164 0.778421  50 MissVal 
5  0.609142  0.384882 0.638997 0.525116 0.496973  50  Rand 

回答

1

你必須先融化的數據,所以列名成爲另一列中的值。

print(df.head(10)) 

    Sensitivity Specificity Accuracy  PPV  AUC k_week model 
0  0.609948  0.867516 0.717291 0.776004 0.813680  4  Sig 
1  0.586824  0.851647 0.685525 0.757557 0.784805  4  MRM 
2  0.581152  0.842510 0.670357 0.749651 0.737269  4  Mean 
3  0.473386  0.601827 0.395841 0.556193 0.542679  4 Rmssd 
4  0.511344  0.717240 0.499148 0.644086 0.626719  4 MissVal 
5  0.454625  0.523924 0.356061 0.508448 0.501661  4  Rand 
0  0.792619  0.764423 0.755839 0.777934 0.835871  6  Sig 
1  0.722077  0.758113 0.733090 0.740845 0.803949  6  MRM 
2  0.633899  0.735877 0.688298 0.687011 0.736115  6  Mean 
3  0.437296  0.613582 0.510095 0.529108 0.544014  6 Rmssd 

df = df.melt(id_vars=['k_week', 'model']) 
print(df.head(10)) 

    k_week model  variable  value 
0  4  Sig Sensitivity 0.609948 
1  4  MRM Sensitivity 0.586824 
2  4  Mean Sensitivity 0.581152 
3  4 Rmssd Sensitivity 0.473386 
4  4 MissVal Sensitivity 0.511344 
5  4  Rand Sensitivity 0.454625 
6  6  Sig Sensitivity 0.792619 
7  6  MRM Sensitivity 0.722077 
8  6  Mean Sensitivity 0.633899 
9  6 Rmssd Sensitivity 0.437296 

grid = sns.FacetGrid(df, row='k_week', col='variable') 
grid.map_dataframe(sns.boxplot, x='model', y='value') 

Boxplot facet grid

+0

驚人的,謝謝!如何從每個字幕中刪除單詞'variable'(但保留'k_week')? –

+1

@ArnoldKlein'grid.set_titles(template ='k_week = {row_name}')'或者'grid.set_titles(col_template ='{col_name}')' – Goyo