2017-07-07 3 views
0

私はパンダに保存された5変数の比較をプロットする必要があります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

方法:

0彼らはDF1に表示されるタイトルは(「CLSによってgroupped箱ひげ図」)
  • が迷惑な[CLS]を取り除くの水平ライン
  • に沿ってプロット
    • の変化をプロットカテゴリを並べ替えますか? (具体続く第1の感度、...)
  • 答えて

    1

    はあなたを助けるかもしれない例です。

    輸入品

    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行/ colsのは、あなたのデータを使用して構築されているサブプロットのグリッドを作成しfactorplotと呼ばれる気の利いたツールを持っています。これを行うには、dfをより使いやすい形に「溶かす」必要があります。

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

    今、私たちはCOL "列" を使用してfactorplotを作成することができます。あなたはまた、単にSeabornの箱ひげ図を使用することができます

    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

    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

    これはあなたに1つの図の代わりに、サブプロットで同じプロットが、すべてを提供します。また、図のタイトルを1行で変更することもできます。残念ながら、私は '列'のサブタイトルを削除する方法を見つけることができませんが、うまくいけば、これはあなたが必要なものを得るでしょう。そう

    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() 
    
    のように、 Factorplot スワップあなたの xy値を row = 'columns'col = 'columns'を変更、 row_order = [...]col_order = [...]を変更し、 '{row_name}''{col_name}'を変更:横にプロットを表示するには

    EDIT

    h factorplot Boxplotあなたのxy値はそのように

    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

    おかげのようなパラメータorient = 'h'を追加スワップ!プロットを縦に表示し、横に表示しない方法はありますか? 1X5のプロットの代わりに、私は転置し、 'factorplot'を使って5X1のプロットを取得する必要がありますか? –

    +0

    はい!私の編集内容を見てください。 –

    1

    多分これは、次のことに役立ちます。私はここでseaborn

    を使用することをお勧め

    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 
    
    関連する問題