First of all, I would like to say that I am new to python and this code has been created alonside advice and suggestions from users on stackoverflow. The code is shown below:
f = open('E:\Python27\WASP DATA\Sample Data.txt',"r")
num=0
line = f.readlines()
X = []
for n, lines in enumerate(line, 0): #6621
# make it 109 to remove the first line "['# Column 3: Magnitude error\n']"
if (n > 109):
linSplit = lines.split(' ')
joined = ' '.join(linSplit)
# apply the float function to every item in joined.split
# create a new list of floats in tmp variable
tmp = map((lambda x: float(x)), joined.split())
X.append(tmp)
#print X[0] # print first element in the list
Period_1 = float(line[28][23:31])
Epoch_1 = float(line[27][22:31])
Period_2 = float(line[44][23:31])
Epoch_2 = float(line[43][22:31])
#Period_3 = float(line[60][23:31])
#Epoch_3 = float(line[59][22:31])
#Period_4 = float(line[76][23:31])
#Epoch_4 = float(line[75][22:31])
#Period_5 = float(line[108][23:31])
#Epoch_5 = float(line[91][22:31])
print("The time periods are:")
print Period_1
print Period_2
#print Period_3
#print Period_4
#print Period_5
print("\nThe Epoch times are:")
print Epoch_1
print Epoch_2
#print Epoch_3
#print Epoch_4
#print Epoch_5
print('respectively.')
P = []
phase_var = float
for j in range(0,len(X),1):
phase_var = (X[j][0] + (10*Period_1) - Epoch_1)/Period_1
P.append(phase_var)
print P[0]
for m in range(0,len(P),1):
P[m]=float(P[m]-int(P[m]))
#print P[0]
Mag = []
for n in range(0,len(X),1):
temp = X[n][1]
Mag.append(temp)
#print Mag[0]
#print X[0]
from pylab import *
#Plotting the first scatter diagram to see if data is phased correctly.
#plot(P, Mag)
scatter(P, Mag)
xlabel('Phase (Periods)')
ylabel('Magnitude')
#title('Dunno yet')
grid(True)
savefig("test.png")
show()
#Bin the data to create graph where magnitudes are averaged, and B lets us mess around with the binning resolution, and reducing effect of extraneous data points.
B = 2050
minv = min(P)
maxv = max(P)
bincounts = []
for i in range(B+1):
bincounts.append(0)
for d in P:
b = int((d - minv) / (maxv - minv) * B)
bincounts[b] += 1
# plot new scatter
scatter(bincounts, Mag)
show()
The original graph is the scatter plot of P and Mag. However there are multiple Mag points for each period time. I am hoping to try to create a new scatter in which I can take all these Y values and average them for each individual X value, therefore creating a tighter graph which has two dips.
I have tried looking at various ways of binning the data, however no matter which method I use, the graph containing the binned data does not seem to display correctly. The X values should run from 0 to 1 like on the pre binned data graph.
This is the data with which I am working, just incase you need to see it.
Can anyone offer any suggestions or advice on how to created the binned data graph? My knowledge of data binning is quite minimal.
Thank you for your time!