I have pandas df as below.
df:
vl ma fg
0 5 20 1
1 6 20 2
2 2 20 3
3 4 30 1
4 9 30 2
5 6 30 3
6 32 40 1
7 7 40 2
8 5 40 3
I have grouped and averaged by "ma":
df2:
ma vl fg
0 20 4.333333 2.0
1 30 6.333333 2.0
2 40 14.666667 2.0
I'd like to plot the mean value df2 as a bar chart, but have the upper and lower levels for "ma" column shown on the bar from df dataframe, something like this:
import pandas as pd
import matplotlib.pyplot as plt
data={"vl": [5,6,2,4,9,6,32,7,5],
"ma": [20,20,20,30,30,30,40,40,40],
"fg": [1,2,3,1,2,3,1,2,3]}
df = pd.DataFrame(data)
df2 =df.groupby("ma", as_index=False).mean()
plt.bar(df2.ma, df2.vl)
plt.show()