0

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:

Output

janicewww
  • 323
  • 1
  • 10

3 Answers3

2

For plotting things like this I often prefer to loop over the dataframe rather than trying to fit things in a group-by or what not.

For example (your data is already in long format as is easier for this approach):

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(3, 3))

for s in df["symbol"].unique():
    dp = df[df["symbol"] == s].copy()
    x = dp["date"]
    y = dp["roe"]
    ax.plot(x, y)

Will give you:

enter image description here

(note - I've created new data so that the lines can be seen)

data used:

          date symbol        roe     roa
0   2019-12-31    FMC   1.763938  5.5051
1   2018-12-31    FMC -11.131745  5.3899
2   2017-12-31    FMC  -4.476314 -1.4457
3   2016-12-31    FMC  -7.981688  2.1289
4   2015-12-31    FMC -14.543511 -3.3608
15  2019-12-31    VMC   3.866193  5.5051
16  2018-12-31    VMC   1.195928  5.3899
17  2017-12-31    VMC  12.469453 -1.4457
18  2016-12-31    VMC   4.784923  2.1289
19  2015-12-31    VMC  13.161288 -3.3608
30  2019-12-31    APD   1.288219  5.5051
31  2018-12-31    APD   3.476883  5.3899
32  2017-12-31    APD  -1.568422 -1.4457
33  2016-12-31    APD  -2.069321  2.1289
34  2015-12-31    APD  -4.012371 -3.3608
45  2019-12-31    MLM  -5.749301  5.5051
46  2018-12-31    MLM   1.204367  5.3899
47  2017-12-31    MLM   9.086242 -1.4457
48  2016-12-31    MLM  13.984092  2.1289
49  2015-12-31    MLM  -4.509655 -3.3608
baxx
  • 3,956
  • 6
  • 37
  • 75
1

Groupby, aggregate, unstack and plot. I am not sure what aggregation you want to use. Say you want to plot sum of roa

df.groupby(['date','symbol'])['roa'].sum().unstack().plot()

enter image description here

or sum of ro and roe

df.groupby(['date','symbol']).sum().unstack().plot()
wwnde
  • 26,119
  • 6
  • 18
  • 32
0

If you have a dataframe, for example:

df=pd.DataFrame({'A':[1,2,4,7,3,0],'B':[10,9,7,5,3,2]})

To plot both columns, can just do:

df.plot()

This will give you:

enter image description here

zabop
  • 6,750
  • 3
  • 39
  • 84