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()
Thanks in advance.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With