2
from scipy import stats
import scipy as sp

g = sns.lmplot(data=df, x='fpw_n', y='AI_PEKEL10_r', hue='River', col="River",
             col_wrap=4, height=3,
             sharex = True, sharey = True,
              )

def annotate(data, **kws):
    r, p = sp.stats.pearsonr(data['fpw_n'], data['AI_PEKEL10_r'])
    ax = plt.gca()
    ax.text(.05, .8, 'r={:.2f}, p={:.2g}'.format(r, p),
            transform=ax.transAxes)
    
g.map_dataframe(annotate(df))

plt.show()

I am trying to show r2 on each subplots. The codes worked before based on Seaborn implot with equation and R2 text. However, it returns error now. It seems map_dataframe part is wrong. Anyone can guide me?

Bo Wang
  • 21
  • 1
  • 1) Can you add enough data to the question to make the example [reproducible](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)? 2) The map dataframe part should probably be: `g.map_dataframe(annotate)`. Otherwise you're just calling the function once. – Nick ODell Dec 30 '21 at 01:44
  • 1
    Thank you, Nick. It worked after your suggestion and I also need to drop the missing values in the rows. – Bo Wang Dec 30 '21 at 02:23

0 Answers0