user2293224
user2293224

Reputation: 2220

Setting same color of a category in both category plots

I am creating two category plots using Seaborn. One category plot has 6 categories whereas the second category plot has 5 categories to plot. There are 3 categories in both plots that are the same. 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. Is there any way of setting the same color of a category that appears in both plots?

Upvotes: 0

Views: 924

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

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 :

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')

enter image description here

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

enter image description here

Upvotes: 1

Related Questions