python-3.x - 为 seaborn 的 factorplot boxplot 中的每个框添加标签
问题描述
我知道有类似的答案,例如this one,但该答案适用于seaborn的箱线图,并且不适用于seaborn的因子图。在一个简单的因子图上:
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)
如何在每个框下方添加注释(在上面的示例中为mean_labels),如下所示:
正如我所说,我尝试使用上面的答案至少尝试获取每个框的位置:
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')
但这会产生:
我相信这是因为我传递了整个情节的轴而不是“因子图”轴。但是我找不到这样做的方法(如果我使用ax=plt.gca()代替,例如示例中的ax=sns.factorplot(...),则会收到错误消息:AttributeError: module ' seaborn' 没有属性 'gca')。
解决方案
推荐阅读
- python - 使用 groupby() 和条件选择 pd.df 中的行
- python - 在这种情况下如何删除代码重复
- visual-studio-code - 包含选项卡的面板的名称是什么?
- corda - Corda 交易链 DAG 或区块链
- python - 如何在字典值发生变化时获取它的频率?
- pycharm - 调试总是给出错误:第 568 行,在 process_net_command ... NameError: jupyter
- mysql - 如何从nodejs连接到mysql?
- python - 如何修复 python3 sendmail 中的“UnicodeEncodeError”
- javascript - 您能否保持一系列父元素的高度相同,同时调整其中一个带有单击的子元素的高度?
- html - 如何将包含宽 HTML 的表格单元格的内容限制为整体特定宽度?