如何在我的水上箱图中添加中位数标签?

时间:2019-02-16 22:55:50

标签: python pandas matplotlib seaborn

我想在箱线图中添加中间标签,但在Seaborn中找不到正确的代码。

我尝试了两种不同的解决方案,分别找到了herehere,但是都没有用。我的代码如下,使用第一种解决方案:

import seaborn as sns
sns.set()
sns.set(rc={'figure.figsize':(40,24)})
sns.set(font_scale=3)
sns.set_style("whitegrid")

ax = sns.boxplot(x="Year", y="Travel Time", data=Potrero_fall_PM_OB_clean, 
showfliers=False).set(xlabel='Year', ylabel='Travel Time (min)', title = 
"Fall PM OB Travel Time on Potrero Ave, Year over Year")

medians = Potrero_fall_PM_OB_clean.groupby(['Year'])['Travel 
Time'].median().values
median_labels = [str(np.round(s, 2)) for s in medians]

pos = range(len(medians))
for tick,label in zip(pos,ax.get_xticklabels()):
    ax.text(pos[tick], medians[tick] + 0.5, median_labels[tick], 
            horizontalalignment='center', size='x-small', color='w', 
            weight='semibold')

我希望得到一个带有中间标签的箱线图,但是我明白了:


AttributeError                            Traceback (most recent call last)
<ipython-input-36-ce75d0ad015b> in <module>
     12 
     13 pos = range(len(medians))
---> 14 for tick,label in zip(pos,ax.get_xticklabels()):
     15     ax.text(pos[tick], medians[tick] + 0.5, median_labels[tick], 
     16             horizontalalignment='center', size='x-small', 
     color='w', weight='semibold')

 AttributeError: 'list' object has no attribute 'get_xticklabels'

我不明白为什么它没有get_xticklabels属性。我该如何解决这个问题并使中位数显示在我的箱线图中?

0 个答案:

没有答案