0

I want to annotate a stacked bar graph using matplotlib.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame()
df['year'] = ['2019','2020','2021','2022']
df = df.set_index('year')
df['test 1'] = [2,16,85,15]
df['test 1a'] = [16,18,9.5,0]
df['test 2'] = [16.5,15,31.5,24]
df['test 3'] = [13.5,34,37,205]
df['test 4'] = [12.5,10,42,96]
df['test 4a'] = [0,44,0,38]
df['test 4b'] = [6,3,8,0]
df['test 4c'] = [0,131,29.5,1]
df['test 5'] = [0,0,87,0]

fig, ax = plt.subplots(1,1,figsize=(9,4))
df.plot(kind='bar', stacked=True, ax=ax)

ax.annotate(xy=('2021',350), xycoords='data', text='revision')

plt.show()

The aim is to print revision above the 2021 bar like:

enter image description here

I tried using the annotate function but the xy argument doesn't accept the string 2021 as the x-coordinate. How do I fix this?

Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75

0 Answers0