Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign/map colors to the points in Seaborn.regplot (Python 3)

I have a seaborn plot that I want to annotate with colors (preferably with a legend as well witht he color mapping). I see that the regplot has a color method. I don't know how to take use of this.

I set up my I tried a few different ways by either giving the color method a dictionary that maps the {index : color} and even added the color values to the dataframe itself.

How can I map the points with the colors I assigned?

np.random.seed(0)

# Create dataframe
DF_0 = pd.DataFrame(np.random.random((100,2)), columns=["x","y"])

# Label to colors
D_idx_color = {**dict(zip(range(0,25), ["#91FF61"]*25)),
               **dict(zip(range(25,50), ["#BA61FF"]*25)),
               **dict(zip(range(50,75), ["#91FF61"]*25)),
               **dict(zip(range(75,100), ["#BA61FF"]*25))}
DF_0["color"] = pd.Series(list(D_idx_color.values()), index=list(D_idx_color.keys()))

# Plot
sns.regplot(data=DF_0, x="x", y="y") #works, plot below

# sns.regplot(data=DF_0, x="x", y="y", color="color") # doesn't work
# ValueError: to_rgb: Invalid rgb arg "color"
# could not convert string to float: 'color'

# sns.regplot(data=DF_0, x="x", y="y", color=DF_0["color"]) # doesn't work
# ValueError: to_rgb: Invalid rgb arg "('#91FF61', '#91FF61', ...

# sns.regplot(data=DF_0, x="x", y="y", color=D_idx_color) # doesn't work
# ValueError: to_rgb: Invalid rgb arg "(0, 1, 2, ...

enter image description here

like image 574
O.rka Avatar asked Jul 13 '16 22:07

O.rka


1 Answers

Use scatter_kws:

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

np.random.seed(0)

# Create dataframe
DF_0 = pd.DataFrame(np.random.random((100,2)), columns=["x","y"])
DF_0['color'] =  ["#91FF61"]*25 + ["#BA61FF"]*25 + ["#91FF61"]*25 + ["#BA61FF"]*25
#print DF_0

sns.regplot(data=DF_0, x="x", y="y", scatter_kws={'c':DF_0['color']})
plt.show()

enter image description here

like image 67
Serenity Avatar answered Sep 28 '22 05:09

Serenity