简体   繁体   English

Python Seaborn:在两个类别图中设置类别的相同颜色

[英]Python Seaborn: setting same color of a category in both category plots

I am creating two category plots using Seaborn.我正在使用 Seaborn 创建两个类别图。 One category plot has 6 categories whereas the second category plot has 5 categories to plot.一个类别图有 6 个类别,而第二个类别图有 5 个类别要绘制。 There are 3 categories in both plots that are the same.两个图中有 3 个类别是相同的。 I want to set up the same color for each of the categories that are common in both plots.我想为两个图中常见的每个类别设置相同的颜色。 I am using sns.set_palette('coolwarm') to set the color of both plots but the same categories in both plots have different colors.我正在使用sns.set_palette('coolwarm')来设置两个图的颜色,但是两个图中的相同类别具有不同的颜色。 Is there any way of setting the same color of a category that appears in both plots?有没有办法设置出现在两个图中的类别的相同颜色?

It should work if you put them together in a single dataframe and use sns.catplot() and separate your plots by using the col= argument :如果您将它们放在一个数据sns.catplot()并使用sns.catplot()并使用col=参数分隔您的图,它应该可以工作:

np.random.seed(111)

d1 = pd.DataFrame({'x':np.random.randint(1,4,50),
                   'y':np.random.randn(50),
                   'z':np.random.choice(['a','b','c','d','e','f'],50),
                   'data':'d1'
                  })

d2 = pd.DataFrame({'x':np.random.randint(1,4,50),
                   'y':np.random.randn(50),
                   'z':np.random.choice(['d','e','f','g','h'],50),
                   'data':'d2'
                  })

df = pd.concat([d1,d2])
df['z'] = pd.Categorical(df['z'],ordered=True)

sns.catplot(data=df,x='x',y='y',hue='z',col='data',palette='coolwarm')

在此处输入图片说明

sns.catplot(data=df,x='x',hue='z',col='data',kind='count',palette='coolwarm')

在此处输入图片说明

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

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