python - Seaborn FacetGrid barplots and hue -
i have dataframe following structure:
interval segment variable value 4 02:00:00 night weekdays 154.866667 5 02:30:00 night weekdays 100.666667 6 03:00:00 night weekdays 75.400000 7 03:30:00 night weekdays 56.533333 8 04:00:00 night weekdays 55.000000 9 04:30:00 night weekends 53.733333 10 05:00:00 night weekends 81.200000 11 05:30:00 night weekends 125.933333 14 07:00:00 morning weekdays 447.200000 15 07:30:00 morning weekends 545.200000 16 08:00:00 morning weekends 668.733333 17 08:30:00 morning weekends 751.333333 18 09:00:00 morning weekdays 793.800000 19 09:30:00 morning weekdays 781.125000 23 11:30:00 noon weekdays 776.375000 24 12:00:00 noon weekdays 741.812500 25 12:30:00 noon weekends 723.000000 26 13:00:00 noon weekends 734.562500 27 13:30:00 noon weekends 763.882353 28 14:00:00 afternoon weekdays 810.411765 31 15:30:00 afternoon weekdays 855.411765 32 16:00:00 afternoon weekdays 824.882353 33 16:30:00 afternoon weekends 768.529412 34 17:00:00 afternoon weekends 790.812500 35 17:30:00 afternoon weekends 809.125000
i want produce faceted grid of bar plots, 1 each variable (weekdays/weekends) , colour bars according "segment" column.
producing 2 bar plots straightforward:
g = sns.facetgrid(melted, col="variable") g.map(sns.barplot,'interval','value')
this produces (i know xlabels wrong, can correct that):
i'm stuck @ colouring bars according "segment". per docs, need add variable when instantiating facetgrid , set palette:
g = sns.facetgrid(melted, col="variable",hue="segment",palette="set3") g.map(sns.barplot,'interval','value')
the bars stacked in front of each other instead of being spread across whole interval. missing here?
i've created gist dataset.
because interval
nested within x
variable (segment
), need tell barplot
of possible levels of x
variable, not drawn on top of each other:
times = df.interval.unique() g = sns.facetgrid(df, row="variable", hue="segment", palette="set3", size=4, aspect=2) g.map(sns.barplot, 'interval', 'value', order=times)
Comments
Post a Comment