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:
JavaScript
x
8
1
def scatter_plot(csvfile,param,exp):
2
for i in range (1,10):
3
df = pd.read_csv('{}{}.csv'.format(csvfile,i))
4
ax = df.plot(kind='scatter',x=param,y ='Adjusted')
5
df.plot.line(x=param,y='Adjusted',ax=ax,style='b')
6
plt.show()
7
plt.savefig('plot/{}/{}'.format(exp,param),dpi=100)
8
But it’s showing me ten plot and only save the last one. Any idea?
Advertisement
Answer
The structure is
- create an axes to plot to
- run the loop to populate the axes
- save and/or show (save before show)
In terms of code:
JavaScript
1
12
12
1
import matplotlib.pyplot as plt
2
import pandas as pd
3
4
ax = plt.gca()
5
for i in range (1,10):
6
df = pd.read_csv( )
7
df.plot( , ax=ax)
8
df.plot.line( , ax=ax)
9
10
plt.savefig( )
11
plt.show()
12