npm
npm

Reputation: 653

Add text annotation to seaborn lmplot

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:

enter image description here

Upvotes: 2

Views: 6322

Answers (1)

DavidG
DavidG

Reputation: 25371

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

Upvotes: 5

Related Questions