0

I am trying to plot multiple columns from the same dataframe using a loop. Table below shows my dataframe:

       Time  Amount  Amount i=2  Amount i=3  Amount i=4
0    20      10          20          30          40
1    10       5          10          15          20
2    15      25          50          75          75

The desired outcome is to have the values for Amount i=2, Amount i=3, and Amount i=4, on the same figure using a loop.

The code below plots each of the 'Amounts' on a seperate plot.

for i in range(range1,range2):
        df.plot(x ='Time', y=['Amount i={}'.format(i)])  

Any help how to plot them on 1 figure greatly appreciated.

3
  • 1
    df.plot(x='Time', y = ['Amount i={}'.format(i) for i in range(r1,r2)]). Commented Oct 4, 2021 at 15:16
  • Thank you, thats exactly what I was after! appreciate it! Commented Oct 4, 2021 at 15:22
  • Hello, @QuangHoang, I was wondering if you knew how to complete this, if I had multiple variables. For example, 'Amount i={},j={}'.format(i,j) for i in range(r1,r2) & j in range(r2,r4). The example here doesnt work. Many thanks Commented Dec 17, 2021 at 0:03

1 Answer 1

1

You can try this:

for i in range(2,5):
    plt.plot(df["Time"], df[f"Amount i={i}"]

# and do not forget 

plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.