1

I am trying to use a custom colour palette to apply distinct values to hue categories in seaborn, but the output colours do not match my inputs. To give an example:

import random
random.seed(1)

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

BLUE = (0, 160/255, 240/255)
YELLOW = (1, 210/255, 25/255)
GREEN = (110/255, 200/255, 5/255)

sns.set_palette(
    palette=[BLUE, YELLOW, GREEN], n_colors=3
)

sns.palplot(sns.color_palette())
plt.show()

df = pd.DataFrame(
    {
        'x': [0, 0, 0, 1, 1, 1, 2, 2, 2],
        'y': [random.random() for _ in range(9)],
        'hue': ['A', 'B', 'C'] * 3
    }
)

sns.barplot(data=df, x='x', y='y', hue='hue')
plt.show()

The palplot shows the colours as expected:

palplot of blue, yellow and green

However, when these are used in the barplot they come out muted:

enter image description here

My guess is that seaborn is interpolating between the colours in the background. Is there any way I can prevent this from happening and just use the discrete colours I have defined?

asongtoruin
  • 9,794
  • 3
  • 36
  • 47

1 Answers1

3

Seaborn automatically draw desaturated plots (I don't know why, it is a VERY strange decision). But plots have the saturation attribute. Just set it to 1:

saturation : float, optional

Proportion of the original saturation to draw colors at. Large patches often look better with slightly desaturated colors, but set this to 1 if you want the plot colors to perfectly match the input color spec.

sns.barplot(data=df, x='x', y='y', hue='hue', saturation=1)

enter image description here

vurmux
  • 9,420
  • 3
  • 25
  • 45