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, ...
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With