Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn - compress violinplot for zero count categories

Tags:

python

seaborn

Is there a simple way to ignore zero count categories when laying out a violinplot. In the example below, there are no cases of 'Yes:Red' and 'No:Green' but the violinplot still plots the "missing" categories. I can see why this should be the default behavior, but is there some way to change the factors used in the hue to suppress this and remove the whitespace?

df = pd.DataFrame(
    {'Success': 50 * ['Yes'] + 50 * ['No'], 
     'Category': 25 * ['Green'] + 25 * ['Blue'] + 25 * ['Green'] + 25 * ['Red'],
     'value': np.random.randint(1, 25, 100)}
)
sns.violinplot(x='Success', y='value', hue='Category', data=df)
plt.show()

enter image description here

Thanks in advance.

like image 410
JimmyT Avatar asked Nov 07 '22 18:11

JimmyT


1 Answers

This is the closest I could get without situation specific cheating like I suggested in the comment.

You can use the FacetGrid in with the sharex = False argument. Then you need the map method and map violinplot with the proper arguments to the FacetGird object. Like so:

g = sns.FacetGrid(df, col="Success",  sharex=False)
g = g.map(sns.violinplot, 'Category','value')
plt.show()

Resulting in this image:

enter image description here

No more empty spaces where empty plots are drawn.

The only downside is the hue argument is currently not working. I will continue to look for a solution that includes the hue in a proper way. The user can still see the actual Category on the x axis. However this is not ideal.

I still hope that the answer in it's current form will help you.

like image 69
error Avatar answered Nov 15 '22 12:11

error