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

2024-10-03 19:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有一些值的pandas数据框。我想用seaborn的条纹图来可视化数据的传播,尽管这是我第一次使用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...  

然后我试着把它画在这里:

^{pr2}$

我得到了以下错误:

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

有什么办法让我完成这个看似简单的任务?在


Tags: 数据方法boxpandas可视化rgbseaborncondition
1条回答
网友
1楼 · 发布于 2024-10-03 19:30:29

似乎你想区分一个点是否是离群值。因此有两种可能的情况,由hit_box_outliers列确定。
您可以将此列用作条带图的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

相关问题 更多 >