使用每个数据点的RGB值矢量在seaborn图中着色数据点

问题描述 投票:1回答:1

我有一个带有一些值的pandas数据帧。我想使用seaborn的stripplot来显示我的数据的传播,虽然这是我第一次使用seaborn。我认为对作为异常值的数据点进行着色会很有趣,所以我为每个值创建了一个包含RGB元组的列。我之前使用过这种方法,我发现它非常方便,所以我很想找到一种方法来使这项工作,因为seaborn非常好。

这就是数据框的外观:

   SUBJECT  CONDITION(num)       hit  hit_box_outliers  \
0      4.0             1.0  0.807692                 0   
1      4.0             2.0  0.942308                 0   
2      4.0             3.0  1.000000                 0   
3      4.0             4.0  1.000000                 0   
4      5.0             1.0  0.865385                 0   

                                         hit_colours  
0  (0.38823529411764707, 0.38823529411764707, 0.3...  
1  (0.38823529411764707, 0.38823529411764707, 0.3...  
2  (0.38823529411764707, 0.38823529411764707, 0.3...  
3  (0.38823529411764707, 0.38823529411764707, 0.3...  
4  (0.38823529411764707, 0.38823529411764707, 0.3...  

然后我尝试在这里绘制它:

sns.stripplot(x='CONDITION(num)', y='hit', data=edfg, jitter=True, color=edfg['hit_colours'])

我收到以下错误:

ValueError: Could not generate a palette for <map object at 0x000002265939FB00>

关于如何实现这项看似简单的任务的任何想法?

python seaborn
1个回答
0
投票

看起来你想要区分一个异常点。因此存在两种可能的情况,这些情况由列hit_box_outliers确定。 您可以将此列用作stripplot的hue。要获取两个事件的自定义颜色,请使用调色板(或颜色列表)。

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

df= pd.DataFrame({"CONDITION(num)" : np.tile([1,2,3,4],25),
                  "hit" :  np.random.rand(100),
                  "hit_box_outliers": np.random.randint(2, size=100)})


sns.stripplot(x='CONDITION(num)', y='hit', hue ="hit_box_outliers", data=df, jitter=True, 
              palette=("limegreen", (0.4,0,0.8)))

plt.show()

enter image description here

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.