I have the following code snippet to generate data and plot the scatters for two facet dimensions (['train','test'], ['type_a','type_b']) as columns and rows.
import altair as alt
import numpy as np
import pandas as pd
from scipy.stats import pearsonr
np.random.seed(0)
df = pd.DataFrame(data=np.random.randn(1000, 1), columns=['A'])
df['B'] = df['A'] + np.random.rand(1000)
df['subset'] = 'test'
df.loc[:500, 'subset'] = 'train'
df['type'] = 'type_a'
df.loc[300:700, 'type'] = 'type_b'
r = df.groupby(['subset', 'type']).apply(lambda x: pearsonr(x['A'], x['B'])[0])
r.name = 'correlation'
r = pd.DataFrame(r)
print(r)
alt.Chart(df).mark_point().encode(x='A', y='B', column='subset', row='type')
Now I want to annotate each facet subplot with the pearson's correlation as calculated using the groupby in pandas.

Is there any way to put this in the upper corner of each panel or even to the title (except for illustrator)?
Thanks! Max
