How can you plot data from a .txt file using matplotlib?
<p>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): </p>

<pre><code>6876.593750  1
6876.302246  1
6876.003418  0
</code></pre>

<p>I would like to plot the data from the txt. file.<br>
The error message is [IndexError: list index out of range]</p>

<p>The code I'm using is: </p>

<pre><code>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()
</code></pre>

<p>Thank you in advance! </p>