Add text annotation to seaborn lmplot

可紊 提交于 2020-01-15 06:44:10

问题


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:


回答1:


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



来源:https://stackoverflow.com/questions/54192085/add-text-annotation-to-seaborn-lmplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!