Seaborn countplot: Use the same colour for each variable name on separate df

Viewed 185

I have a clustered dataset of articles, each article belonging to a certain cluster. I've run sentiment analysis on the whole dataset and I want to make plot for the sentiments in each cluster. My problem is that in cluster 1 for example the fear sentiment is in pink colour, anticipation is in blue etc but in the next cluster fear may be green and anticipation may be pink.

I know that i can force seaborn to use the same colour for every variable in the dataframes and all the plots will turn out blue or red or whatever.

What I want to do is to set a certain colour for every emotion and keep it during the whole iterations of plots.

My code:

for c in range (0,7):
  df=corona_data[corona_data['cluster']==c]
  print("\nCluster: ",c)
  print("\n")
  ax = sns.countplot(x= 'emotion',data=df)
  
  plt.show()

which produces plots like these: plot1 plot2

1 Answers

user the order= parameter to fix the order in which the bars are displayed (and hence their colors)

ax = sns.countplot(x= 'emotion',data=df, order=['negative', 'positive', ...])
Related