I am inspired by the previous post which has the similar issue like me. I have a DataFrame and I want to plot a graph with 5 different legend. However, the output has only 1 output which different from the previous post - Pandas dataframe groupby plot
May I know what is the problem?
My dataframe like this:-
import pandas as pd
import import matplotlib.pyplot as plt
df1 = pd.read_csv('df_ratio_mac.csv')
df1
date symbol roe roa
0 2019-12-31 FMC 21.2189 5.5051
1 2018-12-31 FMC 16.7456 5.3899
2 2017-12-31 FMC -4.9167 -1.4457
3 2016-12-31 FMC 6.5580 2.1289
4 2015-12-31 FMC -11.1408 -3.3608
15 2019-12-31 VMC 21.2189 5.5051
16 2018-12-31 VMC 16.7456 5.3899
17 2017-12-31 VMC -4.9167 -1.4457
18 2016-12-31 VMC 6.5580 2.1289
19 2015-12-31 VMC -11.1408 -3.3608
30 2019-12-31 APD 21.2189 5.5051
31 2018-12-31 APD 16.7456 5.3899
32 2017-12-31 APD -4.9167 -1.4457
33 2016-12-31 APD 6.5580 2.1289
34 2015-12-31 APD -11.1408 -3.3608
45 2019-12-31 MLM 21.2189 5.5051
46 2018-12-31 MLM 16.7456 5.3899
47 2017-12-31 MLM -4.9167 -1.4457
48 2016-12-31 MLM 6.5580 2.1289
49 2015-12-31 MLM -11.1408 -3.3608
fig, ax = plt.subplots(figsize=(20,4))
for key, grp in df1.groupby(['symbol']):
ax.plot(grp['date'], grp['roe'], label=key)
ax.legend()
plt.show()
My Output: