pythonpython-3.xseabornscatter-plotcolor-palette

Custom color palette in seaborn


I have a scatterplot that should show the changes in bond lengths depending on temperature. I wanted to give each temperature a specific color, but it doesn't seem to work - plot uses the default seaborn palette. Is there a way to map temperature to color, and make seaborn use it?

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

palette = ["#090364", "#091e75", "#093885", "#085396", "#086da6", 
           "#0888b7", "#08a2c7", "#07bdd8", "#07d7e8", "#07f2f9", 
           "#f9ac07", "#c77406", "#963b04", "#640303"]

sns.set_style("whitegrid")
sns.set_palette(palette)
plot = sns.scatterplot(df.loc[:,'length'], 
                       df.loc[:,'type'],
                       hue = df.loc[:,'temperature'],
                       legend = False, 
                       s = 200)

Solution

  • 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)