Skip to content Skip to sidebar Skip to footer

Python Plot After String

I want to plot data from .dat file. But this .dat file starts with string. For example, My file includes col 1 col 2 col 3 and I want to read under the col 3 data. I want skip two

Solution 1:

Since you are already importing numpy, you could use np.genfromtext here to make things a lot simpler, since it has the option skip_header which tells it how many header rows to skip.

import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('input.dat', skipheader=2)
xv = data[:, 2]

plt.plot(xv)
plt.show()

Or, if you only need to read in column 3:

import matplotlib.pyplotas plt
import numpy as np

xv = np.genfromtxt('input.dat', skipheader=2, usecols=(2,))

plt.plot(xv)
plt.show()

Solution 2:

You can skip the header lines:

for line in lines[2:]:
    p= line.split()
    x1.append(float(p[3]))

This leaves out the first two lines of you input.dat

Post a Comment for "Python Plot After String"