Seaborn - Squeeze Violin Scarf for Zero Count Categories

Is there an easy way to ignore the zero counting categories when lining up the scripting. In the example below, there are no "Yes: red" and "No: green" cases, but the treble plan still displays the "missing" categories. I can understand why this should be the default behavior, but is there a way to change the factors used in the hue to suppress this and remove 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.

+3


source to share


1 answer


This is the closest I could get without a specific betrayal situation as I suggested in the comment.

You can use FacetGrid

in with an argument sharex = False

. Then you need a method map

and a map violinplot

with the appropriate arguments for the object FacetGird

. For example:

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

      

The result in this image:



enter image description here

More empty spaces where empty areas are drawn.

The only drawback is the tint argument is currently not working. I will continue to look for a solution that contains the tint properly. The user can still see the actual category on the x-axis. However, this is not ideal.

I still hope the answer in this form will help you.

+1


source







All Articles