Matplotlib-条形图上的值标签

Matplotlib-条形图上的值标签,matplotlib,Matplotlib,您好,我想在下面的条形图上添加值标签: df = pd.DataFrame({'Percentile':[25, 50, 75] , "Price in GBP":[ 10.000000 , 50.000000 , 453.750000 ]}) df.plot.bar(x='Percentile', y="Price in GBP", rot=0,grid=True) plt.ylabel("Price in GBP

您好,我想在下面的条形图上添加值标签:

df = pd.DataFrame({'Percentile':[25,         50,       75] , "Price in GBP":[
10.000000 ,
50.000000 ,
453.750000
]})

df.plot.bar(x='Percentile', y="Price in GBP", rot=0,grid=True)
plt.ylabel("Price in GBP")
plt.title("Business Coach - Price Distribution")
plt.show()
图表应如下所示:


我搜索了很多,但遗憾的是找不到有效的相关解决方案。感谢

要向图表添加文本,您需要通过循环数据一次添加一个标签

这些条位于0、1和2,这就是为什么使用
范围(len(df))
而不是
df[“百分位”]
的原因。我还添加了一个偏移量(
-.1
+5
)到
x
y
,以便文本显示在条的中心。尝试删除/调整这些偏移,以查看输出是如何变化的

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Percentile':[25,         50,       75] , "Price in GBP":[
10.000000 ,
50.000000 ,
453.750000
]})

df.plot.bar(x='Percentile', y="Price in GBP", rot=0,grid=True)

# add the labels one at a time
for x, price in zip(range(len(df)), df["Price in GBP"]):
    plt.text(x - .1, price + 5, price)

plt.show()
您可以花费大量时间调整格式,但这应该可以让您开始


此页面可能会有所帮助,如果您在每个栏的顶部显示坐标,则不会有帮助,因为我没有使用坐标