首页 > 解决方案 > 在栏中显示价值

问题描述

我一直在尝试运行以下代码:

df = alc_gasolina[(alc_gasolina['ANO'] == 2009) & (alc_gasolina['MÊS'] == 5)]
ax = sns.barplot(y="PREÇO MÉDIO REVENDA",x="DIA", hue="PRODUTO", data=df )
ax.set(ylim=(1.4,2.6))
for index, row in df.iterrows():
    ax.text(row.DIA,row['PREÇO MÉDIO REVENDA'], round(row['PREÇO MÉDIO REVENDA'],2), color='black', ha="center")

结果是下图:(文本未对齐。)

结果。 标签未对齐。

有人可以帮忙吗?

标签: pythonpandasmatplotlibseaborn

解决方案


使用ax.patches你可以实现它。

东风:

    a   b   c   d
a1  66  92  98  17
a2  83  57  86  97
a3  96  47  73  32

ax = df.T.plot(width=0.8, kind='bar',y=df.columns,figsize=(10,5))
for p in ax.patches:
    ax.annotate(str(round(p.get_height(),2)), (p.get_x() * 1.005, p.get_height() * 1.005),color='green')
  
ax.axes.get_yaxis().set_ticks([])

在此处输入图像描述


推荐阅读