Seaborn 散点图 - 标签数据点[重复]

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

我有一个使用数据框中数据的 Seaborn 散点图。我想使用与该观察(行)关联的 df 中的其他值向绘图添加数据标签。请参见下文 - 有没有办法将至少一个列值(A 或 B)添加到图中?更好的是,有没有办法添加两个标签(在本例中是 A 列和 B 列中的值?)

我尝试使用 for 循环,在我的搜索中使用如下所示的函数,但没有成功使用此散点图。

感谢您的帮助。

df_so = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
scatter_so=sns.lmplot(x='C', y='D', data=df_so,
           fit_reg=False,y_jitter=0, scatter_kws={'alpha':0.2})

fig, ax = plt.subplots() #stuff like this does not work 
python pandas plot seaborn
1个回答
5
投票

用途:

df_so = pd.DataFrame(np.random.randint(0,100,size=(20, 4)), columns=list('ABCD'))
scatter_so=sns.lmplot(x='C', y='D', data=df_so,
           fit_reg=False,y_jitter=0, scatter_kws={'alpha':0.2})


def label_point(x, y, val, ax):
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
    for i, point in a.iterrows():
        ax.text(point['x']+.02, point['y'], str(point['val']))
        
label_point(df_so['C'], df_so['D'], '('+df_so['A'].astype(str)+', '+df_so['B'].astype(str)+')', plt.gca())

输出:

© www.soinside.com 2019 - 2024. All rights reserved.