2015-05-21 14 views
10

Mi piacerebbe tracciare un factorplot in Seaborn ma fornire manualmente le barre di errore invece di far calcolare a Seaborn.Barre di errore personalizzato diagramma del fattore di Seaborn

Ho un dataframe panda che sembra più o meno in questo modo:

 model output feature mean std 
0 first two  a 9.00 2.00 
1 first one  b 0.00 0.00 
2 first one  c 0.00 0.00 
3 first two  d 0.60 0.05 
... 
77 third four  a 0.30 0.02 
78 third four  b 0.30 0.02 
79 third four  c 0.10 0.01 

e sto emettere una trama che sembra più o meno in questo modo: seaborn bar plots

Sto usando questo Seaborn comandi per generare la trama:

g = sns.factorplot(data=pltdf, x='feature', y='mean', kind='bar', 
        col='output', col_wrap=2, sharey=False, hue='model') 
g.set_xticklabels(rotation=90) 

Tuttavia, non riesco a capire come hanno l'uso Seaborn colonna 'std' come le barre di errore. Sfortunatamente, richiederebbe molto tempo per ricalcolare l'output per il frame di dati in questione.

Questo è un po 'simile a questo q: Plotting errors bars from dataframe using Seaborn FacetGrid

meno che non possa capire come farlo funzionare con la funzione matplotlib.pyplot.bar.

C'è un modo per farlo utilizzando Seaborn factorplot o FacetGrid combinato con matplotlib?

Grazie!

+0

Penso che la questione legata sta per essere il modo migliore per andare. 'plt.bar' ha un parametro' yerr' che dovrebbe aiutare. – mwaskom

+0

Grazie a @mwaskom, qualche consiglio su come ottenerlo? attualmente il seguente codice induce: 'g = sns.FacetGrid (data = pltdf, col = 'output', col_wrap = 6, sharey = False, hue = 'model') g.map (plt.bar, 'feature', 'mean', yerr = 'std') ' – crackedegg

+0

scuse per il codice disordinato, non riesco a farlo per essere formattato correttamente nella sezione commenti. – crackedegg

risposta

4

Si potrebbe fare qualcosa di simile

import seaborn as sns 
import matplotlib.pyplot as plt 
from scipy.stats import sem 
tips = sns.load_dataset("tips") 

tip_sumstats = (tips.groupby(["day", "sex", "smoker"]) 
        .total_bill 
        .agg(["mean", sem]) 
        .reset_index()) 

def errplot(x, y, yerr, **kwargs): 
    ax = plt.gca() 
    data = kwargs.pop("data") 
    data.plot(x=x, y=y, yerr=yerr, kind="bar", ax=ax, **kwargs) 

g = sns.FacetGrid(tip_sumstats, col="sex", row="smoker") 
g.map_dataframe(errplot, "day", "mean", "sem") 

enter image description here

+0

Stellar, e grazie mille @mwaskom per il tuo aiuto e il delizioso pacchetto che hai creato. È davvero incredibilmente utile. – crackedegg

+1

@mwaskom Come aggiungo le barre di errore mantenendo la tonalità –

Problemi correlati