Python 3.x 将标签添加到seaborn';s因子图箱线图

Python 3.x 将标签添加到seaborn';s因子图箱线图,python-3.x,seaborn,Python 3.x,Seaborn,我知道也有类似的答案,例如,但这一个适用于seaborn的箱线图,它不适用于我与seaborn的factorplot。在一个简单的factorplot上: import seaborn as sns tips = sns.load_dataset("tips") means = tips.groupby(["sex","smoker","time"])["tip"].mean().values means_labels = [str(int(s)) for s in means] with

我知道也有类似的答案,例如,但这一个适用于seaborn的箱线图,它不适用于我与seaborn的factorplot。在一个简单的factorplot上:

import seaborn as sns

tips = sns.load_dataset("tips")

means = tips.groupby(["sex","smoker","time"])["tip"].mean().values
means_labels = [str(int(s)) for s in means]

with sns.plotting_context("notebook",font_scale=2):
    g = sns.factorplot(x="sex", y="total_bill", hue="smoker",\
                   col="time",  data=tips, kind="box", size=6, aspect=.7)
如何在每个框下方添加注释(在上面的示例中,是means_标签),如下所示:

正如我所说,我尝试使用上述答案,至少尝试获取每个框的位置:

import matplotlib.pyplot as plt
ax = plt.gca()
pos = range(len(means))
for tick,label in zip(pos,ax.get_xticklabels()):
        ax.text(pos[tick], means[tick] + 0.5, meanslabels[tick], 
               horizontalalignment='center', color='r', weight='semibold')
但这会产生:


我相信这是因为我传递的是整个情节的轴,而不是“factorplot”轴。但是我找不到这样做的方法(如果我使用ax=plt.gca()而不是ax=sns.factorplot(…),我会得到错误:AttributeError:module'seaborn'没有属性'gca')。

不,这是因为您传递了
ax.get\xticklabels()
,所以第一个数字位于第一个标签的位置,第二个数字位于第二个ticklabel的位置,对于第三个数字,不再有ticklabel。。。相反,您可以尝试获取框并在其位置上循环,例如,
ax.patches[i]。get_x()
@ImportanceOfBeingErnest我找不到一种方法来循环所有框<当
ax=plt.gca()
仅包含2项时,code>ax.patches。我在
plt.gca()
中看到的唯一属性有两个以上的值,即
lines
有24个值和
artists
有4个值,但我在
ax.artists[I]
上没有看到任何属性可以给出x/y位置。我明白了。很难给seaborn制作的这种黑匣子情节贴上标签。您可以使用
artists
,获取路径并将顶点的平均值作为x值,
np.mean(g.axes[0,0].artists[0].get_path().vertices[:2,0])
。我只是担心这不会有太大帮助,因为您仍然需要知道哪些数据对应于哪个框。您最好使用像映射自定义函数这样的解决方案,其中包括注释。但我目前不知道如何处理这种情况下的色调。