简体   繁体   English

Seaborn Violinplot 更改色调顺序

[英]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.我想要实现的是在每个拆分的右侧有 False 条目,在左侧有 True 条目。 Is there any way to achieve that?有什么方法可以实现吗?

You need to set hue_order :您需要设置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'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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