Home > Back-end >  Label a line graph above each point on the line with its value python
Label a line graph above each point on the line with its value python

Time:12-01

I have a plot with a bar and line graph on different y axes. I need to label the line graph with its values just above each point on the line. I've tried something like this

for p in ax2.patches:
    w,h = p.get_width(), p.get_height()
    x = p.get_x()
    ax2.text(x   w/2, h * 1.1, 
             f'{h}', va='center', ha='center')

which is a structure I used to label the bar graph. I also tried the example listed on this page pic

CodePudding user response:

Try:

for x,y in t["lines"].items():
    plt.annotate(f"{y:.1f}", (x,y), textcoords="offset points", xytext=(0, 5), ha="center")
  • Related