【发布时间】:2022-08-06 01:33:12
【问题描述】:
考虑以下数据和 FacetGrid:
d = {\'SITE\':[\'A\', \'B\', \'C\', \'C\', \'A\'], \'VF\':[0.00, 0.78, 0.99, 1.00, 0.50],\'TYPE\':[\'typeA\', \'typeA\', \'typeB\', \'typeC\', \'typeD\']}
new_df = pd.DataFrame(data=d)
with sns.axes_style(\"white\"):
g = sns.FacetGrid(data=new_df, col=\'SITE\', col_wrap= 3, height=7, aspect=0.25,
hue=\'TYPE\', palette=[\'#1E88E5\', \'#FFC107\', \'#D81B60\'])
g.map(sns.scatterplot, \'VF\', \'TYPE\', s=100)
使用另一个dataframe:
d = {\'SITE\':[\'A\', \'B\', \'C\'], \'N\':[10, 5, 7]}
ann_df = pd.DataFrame(data=d)
其中SITE 与原始new_df[\'SITE\'] 匹配,和与new_df[\'SITE\'] 的维度不同,但在FacetGrid 中有columns 的对应长度。
你如何 annotate 每个 subplot 使用自定义 func 使用不是散点图new_df,但ann_df 或自定义list,如果它匹配原始new_df[\'SITE\'] 并将ann_df[\'N\'] 添加到每个子图,如下所示:
所以,沿着这些路线或更好的东西:
def annotate(data, **kws):
n = data # should be the int for each matching SITE
ax = plt.gca()
ax.text(.1, .2, f\"N = {n}\", transform=ax.transAxes)
g.map_dataframe(annotate(ann_df))
标签: python-3.x pandas matplotlib seaborn plot-annotations