Alex T
Alex T

Reputation: 3754

set yticks colors to match barplot colors python

I'm trying to figure out how to set y names to match the color of the barplot that corresponds to it. So every publisher name has the same color as the bar next to it, like PS2 will be pink, 3DS green and so on.

f, ax=plt.subplots(figsize=(7,7))
sns.barplot(x=gbyplat,y=gbyplat.index, palette='husl')
plt.ylabel('Publisher', color='deepskyblue', size=15, alpha=0.8)
plt.xlabel('Number of titles published', color='slateblue', size=15, alpha=0.7)

image

Upvotes: 1

Views: 389

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

You can iterate over the ticklabels and set the respective color from the palette.

enter image description here

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

l =list("ABCDEFGHIJK")
x = np.arange(1,len(l)+1)[::-1]
f, ax=plt.subplots(figsize=(7,7))
sns.barplot(x=x,y=l, palette='husl', ax=ax)
plt.ylabel('Publisher', color='deepskyblue', size=15, alpha=0.8)
plt.xlabel('Number of titles published', color='slateblue', size=15, alpha=0.7)

p = sns.color_palette("husl", len(l))
for i, label in enumerate(ax.get_yticklabels()):
    label.set_color(p[i])

plt.show()

Upvotes: 1

Related Questions