I figured it out. You had to paste the number of colors into the palette:
sns.set_style("whitegrid")
plot = sns.scatterplot(df.loc[:,'length'],
df.loc[:,'type'],
hue = df.loc[:,'temperature'],
palette=sns.color_palette(palette, len(palette)),
legend = False,
s = 200)
Answer from Anavae on Stack OverflowI figured it out. You had to paste the number of colors into the palette:
sns.set_style("whitegrid")
plot = sns.scatterplot(df.loc[:,'length'],
df.loc[:,'type'],
hue = df.loc[:,'temperature'],
palette=sns.color_palette(palette, len(palette)),
legend = False,
s = 200)
In newer Seaborn versions (≥0.11) you can use:
colors = ["#090364", "#091e75", "#093885", "#085396", "#086da6",
"#0888b7", "#08a2c7", "#07bdd8", "#07d7e8", "#07f2f9",
"#f9ac07", "#c77406", "#963b04", "#640303"]
sns.set_style("whitegrid")
plot = sns.scatterplot(
x='length',
y='type',
hue='temperature',
legend=False,
palette=sns.color_palette(colors),
s=200,
data=df)
The reason why your custom palette isn’t picked by Seaborn is that sns.set_palette() only sets the default color cycle for plots that don't use the hue semantic.
When you use hue=… in Seaborn, data points are categorised using different colors based on the hue variable and its categories (or levels). For instance, if hue='temperature', Seaborn extracts all unique distinct values in the 'temperature' column.
Here’s the thing: you need to specify a color for each of these hue levels and it’s up to you to make sure that your custom palette has the correct number of colors.
From your code I can also tell that you’re using an older version of Seaborn, certainly pre because starting from version 0.11:
Most plotting functions now require all of their parameters to be specified using keyword arguments.
(quote from https://seaborn.pydata.org/whatsnew/v0.11.0.html#required-keyword-arguments)
So the positional arguments
plot = sns.scatterplot(
df.loc[:,'length'],
df.loc[:,'type'],
in newer Seaborn versions should be given as
plot = sns.scatterplot(
x=df.loc[:,'length'],
y=df.loc[:,'type'],
Here's a demo with an extra improvement of the original code (using data=df so that the columns can be specified just by their name):
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Create a simple sample DataFrame
np.random.seed(0)
temperature_levels = [f'Temp{i:02d}' for i in range(14)] # 14 unique temps
df = pd.DataFrame({
'length': np.random.rand(100) * 10,
'type': np.random.rand(100) * 5,
'temperature': np.random.choice(temperature_levels, size=100)
})
# 14 colors
colors = ["#090364", "#091e75", "#093885", "#085396", "#086da6",
"#0888b7", "#08a2c7", "#07bdd8", "#07d7e8", "#07f2f9",
"#f9ac07", "#c77406", "#963b04", "#640303"]
sns.set_style("whitegrid")
plot = sns.scatterplot(
x='length',
y='type',
hue='temperature',
legend=False,
palette=sns.color_palette(colors),
s=200,
data=df)

What happens if the number of colors doesn't match the number of hue levels?
If the palette contains more colors than there are unique temperature values, Seaborn will ignore the extra colors. You may also see a warning like this:UserWarning: The palette list has more values (14) than needed (10), which may not be intended.
If the number of hue levels is larger than the number of colors in your palette, Seaborn will cicle on the colors thus creating unwanted effects. You may also see a warning like this:
The palette list has fewer values (14) than needed (43) and will cycle, which may produce an uninterpretable plot.
Creating Custom Seaborn Color Palettes from ONLY two Hex Codes? [Question]
All seaborn color palettes together, so you don't have to search for them everytime
Python data visualization library seaborn's polite way of asking have I've lost all good chromatic sense
Excell default color palette. Much better than it used to be, but still cringey.
More on reddit.comTIL that seaborn refuses to use the jet color scheme in a most wonderful fashion
Here is why jet is a bad color map: http://jakevdp.github.io/blog/2014/10/16/how-bad-is-your-colormap/
tl;dr; It was once the default for matplotlib, but it sucks because the colors lie to you. The perceived luminance of the gradient jumps all over the place and you might see highlights and features that simply are not there.
More on reddit.comHi,
I am wondering if it is possible to create custom diverging seaborn color palettes by only giving it the starting and end colors.
So as an example, If I wanted Hex Code '#3877C7' to be the negative value and '#C75B38' to be the positive, is there a way to tell seaborn (or anything) this and have it automatically fill in the rest of the colors?
I know you can create custom palettes if you list off every combo you want, but that seems very manual if you are changing the two end colors often.
Thanks!