syntax syntax - 4 months ago 29
Python Question

seaborn FacetGrid , ranked barplot separated on row and col

Given some data:

pt = pd.DataFrame({'alrmV':[000,000,000,101,101,111,111],
'he':[e,e,e,e,h,e,e],
'inc':[0,0,0,0,0,1,1]})


I would like to create a bar plot separated on row and col.

g = sns.FacetGrid(pt, row='inc', col='he', margin_titles=True)
g.map( sns.barplot(pt['alrmV']), color='steelblue')


This, works, but how do I also add:


  1. an ordered x-axis

  2. only display the top-two-by-count
    alrmV
    types



To get an ordered x-axis, that displays the top 2 count types, I played around with this grouping, but unable to get it into a Facet grid:

grouped = pt.groupby( ['he','inc'] )
grw= grouped['alrmV'].value_counts().fillna(0.) #.unstack().fillna(0.)
grw[:2].plot(kind='bar')


Using FacetGrid, slicing limits the total count displayed

g.map(sns.barplot(pt['alrmV'][:10]), color='steelblue')


So how can I get a bar graph, that is separated on row and col, and is ordered and displays only top 2 counts?

Answer

I couldn't get the example to work with the data you provided, so I'll use one of the example datasets to demonstrate:

import seaborn as sns
tips = sns.load_dataset("tips")

We'll make a plot with sex in the columns, smoker in the rows, using day as the x variable for the barplot. To get the top two days in order, we could do

top_two_ordered = tips.day.value_counts().order().index[-2:]

Then you can pass this list to the x_order argument of barplot.

Although you can use FacetGrid directly here, it's probably easier to use the factorplot function:

g = sns.factorplot("day", col="sex", row="smoker",
               data=tips, margin_titles=True, size=3,
               x_order=top_two_ordered)

Which draws:

enter image description here

While I wouldn't recommend doing exactly what you proposed (plotting bars for different x values in each facet), it could be accomplished by doing something like

g = sns.FacetGrid(tips, col="sex", row="smoker", sharex=False)

def ordered_barplot(data, **kws):
    x_order = data.day.value_counts().order().index[-2:]
    sns.barplot(data.day, x_order=x_order)

g.map_dataframe(ordered_barplot)

to make

enter image description here