为seaborn lmplot添加文本注释

npm*_*npm 2 python plot seaborn

我正在尝试为聚类结果创建seaborn lmplot,数据示例如下所示:

    ID   CA     IP  clusters
    38  10.3    5.6   1
    59  10.4    6.1   0
    64  10.0    6.6   1
    35  10.6    5.6   1
    54  10.6    5.6   1
    60  10.2    8.2   1
Run Code Online (Sandbox Code Playgroud)

有两个集群(集群 0 和集群 1),我想在每个 scatter 上显示基于“ID”列的“ID”尝试了在seaborn regplot中添加文本的功能,但出现错误,提示“FacetGrid没有文本功能”。

seaborn 情节的代码:

ax = sns.lmplot('CA', 'IP', 
     data=df_tr, 
     fit_reg=False, 
     hue="clusters",  palette="Set1",
     scatter_kws={"marker": "D", "s": 50})

plt.title('Calcium vs Phosporus')
plt.xlabel('CA')
plt.ylabel('IP')
Run Code Online (Sandbox Code Playgroud)

和情节:

在此输入图像描述

Dav*_*idG 5

问题是seaborn.regplot(在您链接到的站点中使用)返回一个 matplotlib 轴对象,该对象允许您使用该text函数。但是,seaborn.lmplot返回一个 FacetGrid。

因此,您需要获取 Facetgrid 的轴,您可以使用

fgrid = sns.lmplot(...)
ax = fgrid.axes[0,0]  # fgrid.axes return an array of all axes in the figure, so we index the array
Run Code Online (Sandbox Code Playgroud)

从这里您可以使用链接中所示的功能