data = {'name': ['A', 'B', 'C', 'D'],
'score': [-9.5, -8.3, -8.1, -7.0],
'color': [4, 3, 2, 1]}
df = pd.DataFrame(data)
I have my data in a dataframe like above and I am plotting it to a seaborn swarmplot like the one below. The points are plotted based on their score, and depending on how that falls between the 3 dotted lines, I want to color the points differently. I use the 'color' column of the df to assign a key based on where the 'score' values fall that corresponds to colors and a dictionary.
colors = {1:'pink', 2:'orange', 3:'red', 4:'green'}
I then create the swarmplot with the below code and map the color dictionary to the colors column of my df.
ax = sns.swarmplot(data=df, y='score', s=10, c=df['color'].map(colors))
When I do this I don't generate any errors, but no colors are applied, and the points remain their default blue (image below, left). So, how can I assign colors to points in a seaborn swarmplot based on my df['color']
column?
Final note: When I try to use palette=df['color'].map(colors)
instead of c=df['color'].map(colors)
, the graph just changes everything to the last color in my colors
dictionary (image below, right)
Edit: Thank you for your suggestion @Trenton McKinney. It is somewhat successful in that the colors do properly map, but when I include the 'name' column (Its actually Title) for x, as below, my points are plotted like a scatter instead of a swarm plot. But I get an error if I try to remove x='Title'
from my parameters.
ax = sns.swarmplot(data=data, x='Title', y='score', s=10, hue='color', palette=colors)
CodePudding user response: