Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas.plot Multiple plot same figure

I have multiple CSV files that I am trying to plot in same the figure to have a comparison between them. I already read some information about pandas problem not keeping memory plot and creating the new one every time. People were talking about using an ax var, but I do not understand it...

For now I have:

def scatter_plot(csvfile,param,exp):
    for i in range (1,10):
        df = pd.read_csv('{}{}.csv'.format(csvfile,i))
        ax = df.plot(kind='scatter',x=param,y ='Adjusted')
        df.plot.line(x=param,y='Adjusted',ax=ax,style='b')
    plt.show()
    plt.savefig('plot/{}/{}'.format(exp,param),dpi=100)

But it's showing me ten plot and only save the last one. Any idea?

like image 920
Boat Avatar asked Jan 30 '23 03:01

Boat


1 Answers

The structure is

  1. create an axes to plot to
  2. run the loop to populate the axes
  3. save and/or show (save before show)

In terms of code:

import matplotlib.pyplot as plt
import pandas as pd

ax = plt.gca()
for i in range (1,10):
    df = pd.read_csv(...)
    df.plot(..., ax=ax)
    df.plot.line(..., ax=ax)

plt.savefig(...)
plt.show()
like image 190
ImportanceOfBeingErnest Avatar answered Jan 31 '23 20:01

ImportanceOfBeingErnest