你能在 seaborn 的分类散点图中突出显示特定的观察结果吗?

问题描述

我有 8 个类别,我已经用 sns.catplot 绘制了分类散点图。有没有办法突出显示每个类别中的特定观察值,以比较与其他观察值相关的位置?

解决方法

您可以使用文本注释,对 Visual 返回的 annotate 对象的 ax (matplotlib.axes.Axes) 属性使用 FaceGrid 方法。例如,下面的代码注释了正常样本上大于 0.5 的观测值:

seaborn.catplot

enter image description here

您可以查看有关 import pandas as pd import numpy as np import seaborn as sns df = pd.DataFrame(data={'x': range(10),'y':np.random.normal(0,1,size=10)}) df['odd'] = df.x.apply(lambda x: x % 2) g = sns.catplot(data=df,x='x',y='y',hue='odd') df[df.y > .5].apply(lambda p: g.ax.annotate(f'({p.x},{round(p.y,2)})',(p.x,p.y)),axis=1) 方法 here 的更多详细信息。