I am trying to change the order of the color palette used in a scatter plot created using seaborn's scatterplot function, where the points are colored according to the "hue" parameter. The plot should be more informative by changing the color palette order.
My dataframe has a column named 'diff' that contains continuous values ranging between approximately [-0.9, 0.9]. Seaborn automatically categorized my data into 5 categories: (-0.8, -0.4, 0.0, 0.4, 0.8).
I have attempted to swap the elements in the colors list and pass it to the scatterplot function as the 'cmap' parameter, but it did not work and the colors remained in their original order. See below:
# Import colormap from matplotlib library
from matplotlib.colors import LinearSegmentedColormap
new_colors_order = sns.color_palette(n_colors=5)
new_colors_order[0], new_colors_order[2] = new_colors_order[2], new_colors_order[0]
# Define a custom colormap
cmap = LinearSegmentedColormap.from_list("", new_colors_order)
# Plot the data using the custom colormap
plt.figure(figsize=(8,8))
sns.scatterplot(data=df, x="X", y="Y", hue="diff", cmap=cmap)
plt.show()
Setting the new list as the color palette did not work either:
new_colors_order = sns.color_palette(n_colors=5)
new_colors_order[0], new_colors_order[2] = new_colors_order[2], new_colors_order[0]
sns.set_palette(sns.color_palette(new_colors_order))
# Plot the data using the custom colormap
plt.figure(figsize=(8,8))
sns.scatterplot(data=df, x="X", y="Y", hue="diff")
plt.show()
What is the correct way of switching between the first and third element of the color palette for the scatterplot, taking into account that 'diff' is a continuous column?
Seaborn's scatterplot doesn't use a cmap=
parameter. Instead, it uses palette=
. Note that the default palette is different for numeric and categorical hue
values. The most simple way to present a palette, is just a list of colors (this is also what is outputted by sns.color_palette(...)
).
Here is an example:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# make a custom list of colors
new_colors_order = sns.color_palette(n_colors=5)
new_colors_order[0], new_colors_order[2] = new_colors_order[2], new_colors_order[0]
# create some reproducible test data
n = np.arange(1, 301)
theta = n * np.pi * (3 - np.sqrt(5))
df = pd.DataFrame({'X': np.sqrt(n) * np.cos(theta),
'Y': np.sqrt(n) * np.sin(theta),
'diff': np.repeat([*'abcde'], 60)})
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(15, 5))
sns.scatterplot(data=df, x="X", y="Y", hue="diff", s=100, ax=ax1)
ax1.set_title('default categorical palette')
# Plot the data using the custom colormap
sns.scatterplot(data=df, x="X", y="Y", hue="diff", s=100, palette=new_colors_order, ax=ax2)
ax2.set_title('changed palette')
sns.despine()
plt.tight_layout()
plt.show()