Why isn't the color palette applied in seaborn?

Question:

When plotting histplot, I can’t apply the color palette, to have each be a different color in the histogram. What’s the problem? In other plottings, the palette works. Here is my code:

plt.figure(figsize=(15, 8))
sns.histplot(x='age', data=df, bins = 20)
sns.color_palette('plasma', as_cmap=True)
plt.show()

Thanks for the help.

Asked By: Kate

||

Answers:

You should specify the palette in the same visual:

cm = sns.color_palette("plasma",20) #Splitting the palette in the same amount of numbers bins
    
# Plot histogram.
plot = sns.histplot(x='age',data=df, bins=20)

for bin_,i in zip(plot.patches,cm):
    bin_.set_facecolor(i)

plt.show()

enter image description here

Answered By: Celius Stingher