为 seaborn lmplot 添加文本注释 [英] Add text annotation to seaborn lmplot
问题描述
我正在尝试为聚类结果创建 seaborn lmplot,数据示例如下所示:
ID CA IP 集群38 10.3 5.6 159 10.4 6.1 064 10.0 6.6 135 10.6 5.6 154 10.6 5.6 160 10.2 8.2 1
有两个集群(集群 0 和集群 1),我想根据每个散点上的ID"列显示ID".尝试了在
问题在于 seaborn.regplot
(在您链接的站点中使用)返回一个 matplotlib 轴对象,它允许您使用 text
函数.但是,seaborn.lmplot
返回一个 FacetGrid.
因此您需要获取 Facetgrid 的轴,您可以使用
fgrid = sns.lmplot(...)ax = fgrid.axes[0,0] # fgrid.axes 返回图中所有轴的数组,因此我们对数组进行索引
从这里您可以使用链接中显示的功能
I am trying to create seaborn lmplot for a clustering result, data example are shown below:
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
There are two clusters (cluster 0 and cluster 1), and I want to show the "ID" based on "ID" column on each scatter. Tried the function of adding text as in seaborn regplot but there are errors saying "FacetGrid does not have text function".
Codes for seaborn plot:
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')
And the plot:
The problem is that seaborn.regplot
(used in the site you link to) return a matplotlib axes object which allows you to use the text
function. However, seaborn.lmplot
returns a FacetGrid.
Therefore you need to get the axes of the Facetgrid which you can do using
fgrid = sns.lmplot(...)
ax = fgrid.axes[0,0] # fgrid.axes return an array of all axes in the figure, so we index the array
From here you can then use the function as shown in the link
这篇关于为 seaborn lmplot 添加文本注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!