Home > OS >  How do I add value labels on line chart based on groupby()?
How do I add value labels on line chart based on groupby()?

Time:08-11

results = df.groupby('depart')['Survey'].mean().to_frame(name = 'Mean').reset_index()

results.plot(x = 'Unit', y = 'Mean', marker = 'o', figsize=(8,5))
plt.grid(True)
plt.ylim(3.60, 5.00)
plt.show()

how do I add the value labels on top of the markers?

thanks!

CodePudding user response:

One way is using annotate

results.plot(x = 'Unit', y = 'Mean', marker = 'o', figsize=(8,5))
plt.grid(True)
plt.ylim(3.60, 5.00)
ax = plt.gca()
for i, val in enumerate(results['Mean']):
    label = results.loc[i, 'Mean']
    ax.annotate(label, (i, val), ha='center')
plt.show()

enter image description here

  • Related