2017-07-07 101 views
0

我需要比较五个变量,存储在熊猫dataframe。我使用了一个例子from here,它工作,但现在我需要更改轴和标题,但我正在努力这样做。与熊猫并排的箱柜

这里是我的数据:

df1.groupby('cls').head() 
Out[171]: 
    sensitivity specificity accuracy  ppv  auc  cls 
0  0.772091  0.824487 0.802966 0.799290 0.863700  sig 
1  0.748931  0.817238 0.776366 0.785910 0.859041  sig 
2  0.774016  0.805909 0.801975 0.789840 0.853132  sig 
3  0.826670  0.730071 0.795715 0.784150 0.850024  sig 
4  0.781112  0.803839 0.824709 0.791530 0.863411  sig 
0  0.619048  0.748290 0.694969 0.686138 0.713899 baseline 
1  0.642348  0.702076 0.646216 0.674683 0.712632 baseline 
2  0.567344  0.765410 0.710650 0.665614 0.682502 baseline 
3  0.644046  0.733645 0.754621 0.683485 0.734299 baseline 
4  0.710077  0.653871 0.707933 0.684313 0.732997 baseline 

这里是我的代码:

>> fig, axes = plt.subplots(ncols=5, figsize=(12, 5), sharey=True) 
>> df1.query("cls in ['sig', 'baseline']").boxplot(by='cls', return_type='axes', ax=axes) 

,所得照片是:

pictures of results

如何:

  • 改变标题(“箱线图通过电邮宣传CLS”)
  • 摆脱恼人的[CLS]沿水平线
  • 重新排列绘制类别绘制它们出现在DF1? (第一敏感度,其次是SPECI ...)

回答

1

我建议使用seaborn

下面是一个例子,可以帮助你:

进口

import matplotlib.pyplot as plt 
import numpy as np 
import pandas as pd 
import seaborn as sns 

品牌数据

data = {'sensitivity' : np.random.normal(loc = 0, size = 10), 
     'specificity' : np.random.normal(loc = 0, size = 10), 
     'accuracy' : np.random.normal(loc = 0, size = 10), 
     'ppv' : np.random.normal(loc = 0, size = 10), 
     'auc' : np.random.normal(loc = 0, size = 10), 
     'cls' : ['sig', 'sig', 'sig', 'sig', 'sig', 'baseline', 'baseline', 'baseline', 'baseline', 'baseline']} 

df = pd.DataFrame(data) 
df 

Seaborn有一个叫做factorplot极好的工具,创建其中行/ COLS与您的数据建立的次要情节的网格。为了能够做到这一点,我们需要将df“融化”为更有用的形状。

df_melt = df.melt(id_vars = 'cls', 
        value_vars = ['accuracy', 
           'auc', 
           'ppv', 
           'sensitivity', 
           'specificity'], 
        var_name = 'columns') 

现在我们可以使用col“columns”创建factorplot

a = sns.factorplot(data = df_melt, 
        x = 'cls', 
        y = 'value', 
        kind = 'box', # type of plot 
        col = 'columns', 
        col_order = ['sensitivity', # custom order of boxplots 
           'specificity', 
           'accuracy', 
           'ppv', 
           'auc']).set_titles('{col_name}') # remove 'column = ' part of title 

plt.show() 

factorplot

您也可以只使用Seaborn的箱线图。

b = sns.boxplot(data = df_melt, 
       hue = 'cls', # different colors for different 'cls' 
       x = 'columns', 
       y = 'value', 
       order = ['sensitivity', # custom order of boxplots 
         'specificity', 
         'accuracy', 
         'ppv', 
         'auc']) 

sns.plt.title('Boxplot grouped by cls') # You can change the title here 
plt.show() 

boxplot

这会给你同样的情节,都在同一个人物,而不是次要情节。它还允许您用一行更改图形的标题。不幸的是,我找不到一种方法来删除'列'字幕,但希望这会得到你所需要的。

编辑

侧身查看图: Factorplot 交换你xy值,改变col = 'columns'row = 'columns',改变col_order = [...]row_order = [...],并改变'{col_name}''{row_name}'像这样

a1 = sns.factorplot(data = df_melt, 
        x = 'value', 
        y = 'cls', 
        kind = 'box', # type of plot 
        row = 'columns', 
        row_order = ['sensitivity', # custom order of boxplots 
           'specificity', 
           'accuracy', 
           'ppv', 
           'auc']).set_titles('{row_name}') # remove 'column = ' part of title 

plt.show() 

h factorplot Boxplot 更换您的xy值然后添加参数orient = 'h'像这样

b1 = sns.boxplot(data = df_melt, 
       hue = 'cls', 
       x = 'value', 
       y = 'columns', 
       order = ['sensitivity', # custom order of boxplots 
         'specificity', 
         'accuracy', 
         'ppv', 
         'auc'], 
       orient = 'h') 

sns.plt.title('Boxplot grouped by cls') 
plt.show() 

h boxplot

+1

的感谢!有没有办法垂直显示图而非水平?我不需要1X5的情节,而是需要转置并用'factorplot'获得5X1情节? –

+0

是的!请参阅我的编辑。 –

1

也许这可以帮助你:

fig, axes = pyplot.subplots(ncols=4, figsize=(12, 5), sharey=True) 
df.query("E in [1, 2]").boxplot(by='E', return_type='axes', ax=axes, column=list('bcda')) # Keeping original columns order 
pyplot.suptitle('Boxplot') # Changing title 
[ax.set_xlabel('') for ax in axes] # Changing xticks for all plots