将颜色分配/映射到Seaborn.regplot中的点(Python 3)

我有一个seaborn情节,我想用颜色注释(最好是带有一个图例以及颜色映射).我看到regplot有一种颜色方法.我不知道怎么用这个.

我设置了我尝试了几种不同的方法,通过给color方法提供一个映射{index:color}的字典,甚至将颜色值添加到数据帧本身.

如何用我指定的颜色映射点?

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

解决方法:

使用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

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...