在给定条件下向 seaborn 回归图中的某些数据点添加标签

Adding labels to some datapoints in seaborn regression plot given condition

这是一个table:

dict1 = {'left':[7,3,5,10,9],
         'right':[2,17,0,8,1]}
table = pd.DataFrame(dict1)

我创建了一个回归散点图(具有最佳拟合线的散点图):

sns.regplot(x=table['right'], y=table['left'], data=table)

我想给图中的数据点添加标签,其中任一列的值都是 => 10。不知道该怎么做。

您可以遍历 x,y 对,如果有 >=10,则将该文本添加到图表中那些坐标处,偏移量为 +/- .5,这样它就不会落在点上。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
dict1 = {'left':[7,3,5,10,9],
         'right':[2,17,0,8,1]}
table = pd.DataFrame(dict1)

ax = sns.regplot(x=table['right'], y=table['left'], data=table)

for x in table.values:
    if any([n>=10 for n in x]):
        ax.text(x=x[1]+.5, y=x[0]-.5, s=','.join(map(str,reversed(x))))