I am new into the field of plotting in the same figure multiple lines from different data files with matplotlib in python. Currently I have the following script which is to plot column 1 and 2 from file 'statistics_paa_f0_vs_N.dat'. I want to be able also to plot in the same figure column 1 and 2 from file 'statistics_paa_f1_vs_N.dat'
#--- Import the necessary packages and modules
import matplotlib.pyplot as plt
import numpy as np
#--- initiate the list of coordinates for x and y lists
x,y = [],[]
#--- iterate over each line of the file and store it as a string
for line in open('statistics_paa_f0_vs_N.dat','r'):
values = [float(s) for s in line.split()]
x.append(values[0])
y.append(values[1])
#--- plot the data
plt.plot(x,y,color='r',label='line1') # r - red colour
plt.xlabel('time (s)',fontsize=12)
plt.ylabel('Density (kg/m3)',fontsize=12)
plt.title('hi',fontsize=20,fontweight='bold')
plt.legend(loc='upper right')
plt.grid(False)
plt.axis(True)
#--- show and save the plot
plt.savefig('png_files-matplotlib/test.png')
plt.show()
pandasmakes it really easy to read in files of different formats. Can also be used for plotting afterwards.