Home > Mobile >  Plot multiple plots on same figure using loops in pandas
Plot multiple plots on same figure using loops in pandas

Time:10-05

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.

CodePudding user response:

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()
  • Related