2016-12-02 3 views
2

Ich versuche, einen Datensatz mit stripplot zu plotten. Hier ist der Kopf (es gibt 25 Spalten):Plotten einer Wideform-Matrix mit Farben im Seaborn Stripplot

Labels Acidobacteria Actinobacteria Armatimonadetes Bacteroidetes 
0  0    0    495    NaN   27859 
1  1    0   1256    NaN   46582 
2  0    0   1081    NaN   23798 
3  1    0   2523    NaN   35088 
4  0    0   1383    NaN   19338 

ich diesen Datensatz in einem Pandas Datenrahmen gespeichert haben und es zeichnen kann mit:

def plot(): 
    ax = sns.stripplot(data = df) 
    ax.set(xlabel='Bacteria',ylabel='Abundance') 
    plt.setp(ax.get_xticklabels(),rotation=45) 
    plt.show() 

this plot herzustellen.

Ich möchte die Farben auf die 'Labels' Spalte reflektieren. Wenn ich versuche:

sns.stripplot(x=df.columns.values.tolist(),y=df,data=df,hue='Labels') 

ich:

ValueError: cannot copy sequence with size 26 to array axis with dimension 830 

Antwort

3

Also habe ich es aus. Ich musste meine Daten durch Stapeln und Neuindexierung neu ordnen:

cols = df.columns.values.tolist()[3:] 
stacked = df[cols].stack().reset_index() 
stacked.rename(columns={'level_0':'index','level_1':'Bacteria',0:'Abundance'},inplace=True) 

Welche Ausgänge:

  index   Bacteria Abundance 
0   0  Acidobacteria 0.000000 
1   0 Actinobacteria 0.005003 
2   0 Armatimonadetes 0.000000 
3   0  Bacteroidetes 0.281586 

Als nächstes hatte ich eine neue Spalte erstellen Etiketten zu jedem Datenpunkt zuzuweisen:

label_col = np.array([[label for _ in range(len(cols))] for label in df['Labels']]) 
label_col = label_col.flatten() 

stacked['Labels'] = label_col 

So jetzt:

index   Bacteria Abundance Labels 
0  0 Acidobacteria 0.000000  0 
1  0 Actinobacteria 0.005003  0 
2  0 Armatimonadetes 0.000000  0 
3  0 Bacteroidetes 0.281586  0 
4  0  Chlamydiae 0.000000  0 

Und dann Plot:

def plot(): 
    ax = sns.stripplot(x='Bacteria',y='Abundance',data=stacked,hue='Labels',jitter=True) 
    ax.set(xlabel='Bacteria',ylabel='Abundance') 
    plt.setp(ax.get_xticklabels(),rotation=45) 
    plt.show() 
plot() 

Um this graph zu produzieren.

Danke für die Hilfe!

+0

Große Antwort! Aber das ist eine Menge Arbeit, nur um eine breite Matrix mit eingefärbter Farbtonfarbe zu erhalten ... Ich wünschte, Seaborn hätte mehr Unterstützung für Wide-Form-Grafik. –

Verwandte Themen