syntax syntax - 1 year ago 234
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],

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

g = sns.FacetGrid(pt, row='inc', col='he', margin_titles=True) 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

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

Using FacetGrid, slicing limits the total count displayed['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 Source

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 =[-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,

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 =[-2:]
    sns.barplot(, x_order=x_order)


to make

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download