在2x2子图中,将每个条形图上方的所有条形图值标记为

时间:2019-05-31 17:59:59

标签: python pandas matplotlib plot seaborn

我有一个由4个Seaborn条形图组成的2x2子图。我想将每个图表中每个柱的值添加到每个柱的顶部。

我可以尝试添加文本并手动放置文本,但是我知道这样做效率低下,必须有更好的方法。

我似乎无法弄清楚如何获得其他人找到的解决方案,以使各个条形图可以在子图中使用。

这是我的代码:

数据:

vis = pd.DataFrame({'Win %': [52.631579, 59.736842, 50.657895, 42.609649],
                    'Made Playoffs': [7,12,8,4],
                    'Won NFC Championship': [0,2,3,0],
                    'Won Super Bowl': [0,1,2,0]},
                    index=['Cowboys','Eagles','Giants','Redskins'])
vis = test.reset_index()
vis = test.rename(columns={'index':'Team'})

绘图:

fig, ((ax1,ax2), (ax3,ax4)) = plt.subplots(2,2, sharex=True, sharey=False, figsize=(10,10))

fig.suptitle('NFC East Division Rivals, 2000-2018')
fig.subplots_adjust(wspace=0.5, hspace=0.3)

for ax in plt.gcf().get_axes():
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_visible(True)

sns.set_style("darkgrid", {'font.sans-serif':'sans-serif', 'text.color':'#1e1e1e'})

# Win %
sns.barplot(x='Team',y='Win %',data=vis, palette=colors, 
            edgecolor=edges, linewidth=2, ax=ax1)

ax1.title.set_text('Average Win %')
ax1.set_ylabel('')
ax1.set_xlabel('')

for i, v in enumerate(df['Win %']):
    ax1.text(v + 3, i +.25, str(v))


# Playoff Appearances
sns.barplot(x='Team', y='Made Playoffs', data=vis, palette=colors,
           edgecolor=edges, linewidth=2, ax=ax2, estimator=sum)

ax2.title.set_text('Playoff Appearances')
ax2.set_ylabel('')
ax2.set_xlabel('')

# NFC Championships
sns.barplot(x='Team', y='Won NFC Championship', data=vis, palette=colors,
           edgecolor=edges, linewidth=2, ax=ax3, estimator=sum)

ax3.title.set_text('NFC Championships Won')
ax3.set_ylabel('')
ax3.set_xlabel('')
ax3.set_yticks([0, 1, 2, 3])

# Super Bowls
sns.barplot(x='Team', y='Won Super Bowl', data=vis, palette=colors,
           edgecolor=edges, linewidth=2, ax=ax4, estimator=sum)

ax4.title.set_text('Super Bowls Won')
ax4.set_ylabel('')
ax4.set_xlabel('')
ax4.set_yticks([0, 1, 2])

plt.show()

我得到了期望的子图,但是我不知道如何添加一个“标注”值,该值显示每个子图中每个条上方的条的值。

例如,在“平均获胜百分比”条形图中,“牛仔”条上方应为“ 52.6%”,在“老鹰队”上方应为“ 59.7%”,依此类推。

y轴的单位也不同。

非常感谢您的帮助!

0 个答案:

没有答案
相关问题