How can you plot data from a .txt file using matplotlib?
You're just reading in the data wrong. Here's a cleaner way:
with open('Alpha_Particle.txt') as f:
lines = f.readlines()
x = [line.split()[0] for line in lines]
y = [line.split()[1] for line in lines]
x
['6876.593750', '6876.302246', '6876.003418']
y
['1', '1', '0']
maybe you can use pandas or numpy
import pandas as pd
data = pd.read_csv('data.txt',sep='\s+',header=None)
data = pd.DataFrame(data)
import matplotlib.pyplot as plt
x = data[0]
y = data[1]
plt.plot(x, y,'r--')
plt.show()
this is my data
1 93
30 96
60 84
90 84
120 48
150 38
180 51
210 57
240 40
270 45
300 50
330 75
360 80
390 60
420 72
450 67
480 71
510 7
540 74
570 63
600 69
The output looked like this
With Numpy, you can also try it with the following method
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('data.txt')
x = data[:, 0]
y = data[:, 1]
plt.plot(x, y,'r--')
plt.show()