Reputation: 283
Is it possible, in a Seaborn scatterplot that uses hue
to identify categories and a specific column to define the marker size, to allow for the "shade" of the marker color to vary within a category (as defined by hue
) according to a third column?
The following example uses hue
to identify categories
(according to df['cat']
) and df['p']
to vary the marker size. However, it does not allow the shade of the marker color to vary within a category (as defined by hue
). I would like to do this using df['q']
.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
df = pd.DataFrame({'x': np.random.normal(size=50),
'y': np.random.normal(size=50),
'cat': np.random.randint(1,4,size=50),
'p': np.random.uniform(size=50),
'q': np.random.uniform(size=50)})
df.head()
fig, ax = plt.subplots(figsize=(8,8), tight_layout=True)
ax = sns.scatterplot(data = df, x='x',y='y', hue = 'cat', size='p')
plt.show()
Upvotes: 0
Views: 334
Reputation: 16147
The hardest part is getting the right color palettes. You have to pick palettes where the lightest and darkest colors are different enough from the others to tell them apart. I chose the cubehelix approach for this example but you'll run into trouble with more categories.
You'd probably need to define your own set of color palettes and make sure you can tell them apart at any point on the color band.
This just loops over every category and plots one over the other, changing the color scheme as it goes.
plt.figure(figsize=(15,15))
sns.set_style("white")
edge_color = ['blue','brown','green']
for i, x in enumerate(df.cat.unique()):
t = df[df['cat']==x]
p = sns.cubehelix_palette(start=i, rot=0, dark=.1, light=.7, as_cmap=True)
g = sns.scatterplot(data = t, x='x',y='y', hue = 'q', size='p', palette=p, legend=None, edgecolor=edge_color[i])
plt.legend(title='Category', loc='upper left', labels=df.cat.unique())
leg = g.get_legend()
leg.legendHandles[0].set_color('Blue')
leg.legendHandles[1].set_color('Brown')
leg.legendHandles[2].set_color('Green')
Upvotes: 1