1

I've got a dataframe that looks like this:

Symbol  Date    Amount  Portfolio Value
0   BLL 2020-01-06  5116.83 5069.42
1   BLL 2020-03-16  5104.98 4615.97
5   BLL 2020-03-17  5116.83 5118.95
6   BLL 2020-06-15  5104.95 5401.712
7   BLL 2020-06-16  5116.83 5570.23
11  IBB 2014-03-04  542.98  179.48
12  IBB 2014-10-10  810.97  262.33
13  IBB 2016-09-30  810.71  289.45
14  IBB 2016-10-03  810.97  290.08
15  IBB 2016-12-29  810.69  267.19
16  IBB 2016-12-30  810.97  265.55  
52  SCHE2017-07-05  1915.76 1922.68
53  SCHE2017-08-02  1942.2  2063.10
54  SCHE2017-12-22  1891.79 2158.26
57  SCHE2017-12-26  1942.2  2208.64
60  SCHE2018-12-17  1892.65 1870.20
63  SCHE2018-12-18  1942.2  1927.48

How do I create multiple plots for each stock in the column "Symbol" that graphs both the "Amount" column and the "Portfolio Value" by the date?

Here's what I've got so far:

grouped = df.groupby(['Symbol'])
for key, group in grouped:
    group.plot('Date', 'Amount', label = key)

Which gives me a separate plot for each Symbol that looks like:

enter image description here

I for whatever reason, cannot find out how to plot another column (y2) on the same graph. How do I get this for each Symbol:

enter image description here

deanht11
  • 25
  • 4
  • I would guess something like "hold on" since that is how you do multiple lines on the same plot in Matlab and matplotlib is based on that. – Tadhg McDonald-Jensen Aug 08 '20 at 19:50
  • try this inside the for loop: `group.set_index('Date')[['Amount','Portfolio Value']].plot(label = key)` – Terry Aug 08 '20 at 19:57
  • I'm not using matplotlib currently. I'm using Spyder as my IDE and it will display the plot at the top right of the screen when I run the program. Think the group.plot is in the pandas library, but I could be totally wrong. – deanht11 Aug 08 '20 at 19:59
  • 1
    @Terry That worked! Thanks! I also added title = key and it gave me a title header for each symbol. – deanht11 Aug 08 '20 at 20:03
  • Nice!, I will create a answer with this, ok? – Terry Aug 08 '20 at 20:04
  • Thank you kind stranger! – deanht11 Aug 08 '20 at 20:09

2 Answers2

1

Base on this answer, you can set the Date column as index using set_index() select the 2 columns you want to plot together and then call the .plot

grouped = df.groupby(['Symbol'])
for key, group in grouped:
    group.set_index('Date')[['Amount','Portfolio Value']].plot(title = key)
Terry
  • 2,761
  • 2
  • 14
  • 28
0

Try out the below given piece of code. You just need to put as many columns into a list and assign to y-axis:

group.plot(x='Date', y=['Amount', 'Portfolio Value'], title=key)