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:
However, when these are used in the barplot
they come out muted:
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?