'Create multiple plots based on the entry of another column

I want to create a loop to plot the data in column "B" based on the value in column "A", so one plot for the "B" data in 2020 and a separate one for the data in 2021.

A B
2020 10
2020 15
2021 18
2021 20
2021 15

What i tried so far, but didnt really work out, since plots are just empty:

for A, data in df.groupby('A'):
    plt.plot(df.query('A=="{A}"')['B'], label=A)
    plt.savefig('plot_{}.png'.format(A))
    plt.show()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source