简体   繁体   中英

Seaborn Violinplot change order of hue

I'm trying to draw split violins to compare them across the hue variable. The plotting itself works fine, but I would like to have the order of the hue variables changed (ie 'True' being on the left side of the split instead of on the right). A small working example is this:

import pandas as pd
import seaborn as sns
df = pd.DataFrame({'my_bool': pd.Series(np.random.choice([True,False],20),dtype='bool'),
                'value': pd.Series(np.random.choice(200,20),dtype='int'),
                'my_group': np.random.choice(['A','B'],20)})
sns.violinplot(x='my_group',data=df,y='value',hue='my_bool',split=True, palette = {True:'blue', False:'red'})

This is how the output looks like:

What I want to achieve is having the False entries on the right of each split, the True entries on the left. Is there any way to achieve that?

You need to set hue_order :

import pandas as pd
import seaborn as sns
import numpy as np

df = pd.DataFrame({'my_bool': pd.Series(np.random.choice([True,False],20),dtype='bool'),
                'value': pd.Series(np.random.choice(200,20),dtype='int'),
                'my_group': np.random.choice(['A','B'],20)})
sns.violinplot(x='my_group',data=df,y='value',hue='my_bool', hue_order= [True, False], split=True, palette = {True:'blue', False:'red'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM