Reputation: 230
I am passing a pandas dataframe to be plotted with pd.scatterplot
and want to use the 'bright'
color palette. The color is to be determined by values in an integer Series I pass as hue
to the plotting function.
The problem is that this only works when the hue
Series has only two distinct values. When it has only one ore more than 2 different values, the plotting defaults to a beige-to-purple color palette.
When setting the color palette using sns.set_palette('bright')
everything happens as described above. But when I do palette='bright'
inside the plotting function call (and n_classes is != 2) I get an explicit Value Error thrown:
ValueError: Palette {} not understood
Here is the code for reproducing:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_palette('bright') # first method
n_classes = 3
a = np.arange(10)
b = np.random.randn(10)
c = np.random.randint(n_classes, size=10)
s = pd.DataFrame({'A': a, 'B':b, 'C': c})
sns.scatterplot(data=s, x='A', y='B', hue='C')
plt.show()
For the second method simply change the scatterplot call to
sns.scatterplot(data=s, x='A', y='B', hue='C', palette='bright')
Is there a way to get multiple hue levels in the palette I want? Am I doing anything wrong or is this a bug?
Upvotes: 1
Views: 8854
Reputation: 815
You need to pass the number of colors
Something like that.
sns.scatterplot(data=s,
x='A',
y='B',
hue='C',
palette=sns.color_palette('bright', s.C.unique().shape[0])
)
Upvotes: 4