我想在箱线图中添加中间标签,但在Seaborn中找不到正确的代码。
我尝试了两种不同的解决方案,分别找到了here和here,但是都没有用。我的代码如下,使用第一种解决方案:
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属性。我该如何解决这个问题并使中位数显示在我的箱线图中?