2017-07-07 11 views
0

Ich muss einen Vergleich von fünf Variablen, in Pandas gespeichert dataframe. Ich habe ein Beispiel verwendet from here, es hat funktioniert, aber jetzt muss ich die Achsen und Titel ändern, aber ich habe Mühe, dies zu tun.Side-by-Side Boxplots mit Pandas

Hier ist meine Daten:

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 

Hier ist mein Code:

>> 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) 

und die daraus resultierenden Bilder sind:

pictures of results

Gewusst wie:

  • Änderung der Titel ('Boxplot von cls groupped')
  • von lästigen [cls] loszuwerden aufgetragen entlang der horizontalen Linie
  • die geplottet Kategorien neu anordnen, wie sie in df1 erscheinen? (Erste Empfindlichkeit, durch spezifische gefolgt ...)

Antwort

1

ich mit empfehlen seaborn

Hier ist ein Beispiel, die Ihnen helfen:

Importe

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

Marke

Daten
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 hat ein schickes Tool namens factorplot, die ein Raster von Nebenhandlungen erzeugt, wo die Zeilen/Spalten mit Ihren Daten erstellt werden. Um dies tun zu können, müssen wir die df in eine brauchbarere Form "schmelzen".

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

Jetzt können wir die factorplot mit der col "Spalten" erstellen.

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

können Sie auch nur boxplot der 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

Dies wird Ihnen die gleiche Handlung geben, aber alle in einer Figur statt Nebenhandlungen. Außerdem können Sie den Titel der Figur mit einer Linie ändern. Leider kann ich keinen Weg finden, den 'Spalten'-Untertitel zu entfernen, aber hoffentlich wird dir das helfen, was du brauchst.

EDIT

die Plots seitlich an: Factorplot Tauschen Sie Ihre x und y Werte, col = 'columns' zu row = 'columns' ändern, ändern col_order = [...]-row_order = [...] und '{col_name}' wie zu '{row_name}' ändern, so

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 Tauschen Sie Ihre x und y Werte dann den Parameter h boxplot

+1

dank orient = 'h' wie so

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() 

hinzuzufügen! Gibt es eine Möglichkeit, Diagramme vertikal und NICHT horizontal anzuzeigen? Anstatt 1X5-Plots zu haben, muss ich transponieren und 5x1-Plot mit "factorplot" erhalten? –

+0

Ja! Bitte sehen Sie meine Änderungen. –

1

Vielleicht hilft Ihnen:

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 
Verwandte Themen