-1

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.

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
Kate
  • 133
  • 7
  • `sns.color_palette` returns a list of colours; you need to specify that the plot should render with those specific colours. – ifly6 Aug 04 '22 at 13:23
  • It is for histplot that this does not work, the graph remains the standard blue color. – Kate Aug 04 '22 at 13:45
  • 1
    Histplot only supports coloring different hue categories. You could use np.histogram to calculate the values and then plot via sns.barplot. – JohanC Aug 04 '22 at 13:50

1 Answers1

1

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

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53