5

I am currently implementing a code for facetgrid with subplots of barplots with two different groups ('type'), respectively. I am intending to get a plot, where the different groups are not stacked and not overlapping. I am using following code

g = sns.FacetGrid(data,
            col='C',
            hue = 'type',
            sharex=False,
            sharey=False,
            size=7,
            palette=sns.color_palette(['red','green']),
            )
g = g.map(sns.barplot, 'A', 'B').add_legend()

The data is a pandas long format df with following example structure:

data=pd.DataFrame({'A':['X','X','Y','Y','X','X','Y','Y'],
               'B':[0,1,2,3,4,5,6,7],
               'C':[1,1,1,1,2,2,2,2],
               'type':['ctrl','cond1','ctrl','cond1','ctrl','cond1','ctrl','cond1']}
                )

In the created barplots I get now fully overlapping barplots of the two groups, thus ctrlis missing, see below. However, I am intending to get neighbouring non-overlapping bars each. How to achieve that? My real code has some more bars per plot, where you can see overlapping colors (here fully covered)

enter image description here

Rockbar
  • 1,081
  • 1
  • 20
  • 31

2 Answers2

9

this answer shows up how to use FacetGrid directly.

But, if you have 0.9.0 installed, I would recommend you make use of the new catplot() function that will produce the right (at least I think?) plot. Note that this function returns a FacetGrid object. You can pass kwargs to the call to customize the resulting FacetGrid, or modify its properties afterwards.

g = sns.catplot(data=data, x='A', y='B', hue='type', col='C', kind='bar')

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • I don't think it's a bug. The hue of the facetgrid is correctly applied, i.e. it plots one barplot for each category in hue. The expectation to get a grouping *within* a single barplot would be satisfied by using the `hue` of the `barplot` instead. – ImportanceOfBeingErnest Oct 05 '18 at 21:26
  • You're probably right (as your answer clearly shows). I had tried to pass the hue parameter to barplot as well, bit I left it in the FacetGrid creation, and that didn't work either. In any case, I always find FacetGrid cumbersome to use. The new catplot is much easier to use. – Diziet Asahi Oct 05 '18 at 21:41
  • `FacetGrid` is definitely not very transparent. I would suggest you remove the sentence about this being a bug, rendering this into a perfectly acceptable solution. – ImportanceOfBeingErnest Oct 05 '18 at 21:55
6

I think you want to provide the hue argument to the barplot, not the FacetGrid. Because the grouping takes place within the (single) barplot, not on the facet's level.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

data=pd.DataFrame({'A':['X','X','Y','Y','X','X','Y','Y'],
               'B':[0,1,2,3,4,5,6,7],
               'C':[1,1,1,1,2,2,2,2],
               'type':['ctrl','cond1','ctrl','cond1','ctrl','cond1','ctrl','cond1']})

g = sns.FacetGrid(data,
            col='C',
            sharex=False,
            sharey=False,
            height=4)
g = g.map(sns.barplot, 'A', 'B', "type", 
          hue_order=np.unique(data["type"]), 
          order=["X", "Y"], 
          palette=sns.color_palette(['red','green']))
g.add_legend()

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712