I want to plot a txt file using matplotlib but I keep getting this error message. I'm not that familiar with python, as I started learning a couple of weeks ago. The text file is formatted like (it is 2048 rows long):
6876.593750 1
6876.302246 1
6876.003418 0
I would like to plot the data from the txt. file.
The error message is [IndexError: list index out of range]
The code I'm using is:
import numpy as np
import matplotlib.pyplot as plt
with open("Alpha_Particle.txt") as f:
data = f.read()
data = data.split('\n')
x = [row.split(' ')[0] for row in data]
y = [row.split(' ')[1] for row in data]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_title("Plot title")
ax1.set_xlabel('x label')
ax1.set_ylabel('y label')
ax1.plot(x,y, c='r', label='the data')
leg = ax1.legend()
plt.show()
Thank you in advance!