-2

Hi I would like to put value labels on the bar graph below:

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()

The graph should look like this:

enter image description here

I have searched a lot, but sadly can't find a relevant solution that works. Thanks

Ray92
  • 439
  • 6
  • 18
  • This page could help, if you show the coordinates at the top of each of your bars https://stackoverflow.com/questions/22272081/label-python-data-points-on-plot – Jack Morgan Jan 20 '21 at 13:22
  • This does not help since I am not using coordinates – Ray92 Jan 20 '21 at 13:59

1 Answers1

0

To add text to a chart, you'll need to add each label one at a time by looping through the data.

The bars are located at 0, 1, and 2, which is why range(len(df)) is used rather than df["Percentile"]. I also added in an offset (-.1 and +5) to x and y so that the text appears centered over the bar. Experiment with removing/adjusting those offsets to see how the output changes.

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()

You can spend a ton of time adjusting the formatting, but this should get you started.

enter image description here

daronjp
  • 684
  • 8
  • 9