Dear, kind python experts,
I'm quite new to this program having only started 2 days ago, but the program I want appears to be relatively simple and I've managed to copy most of the important code from other examples. In a nutshell, I want to
1) Open a txt file with 3 columns (time, x, and y) and convert them into lists
2) Animate a graph showing a line moving to x and y in real time.
I've managed to cobble together both types of programs.
the first basically looks like this
def xPoints():
count = 0
xlist = []
lines = open('./test.txt')
for line in lines:
split_line = re.split(',\W*',line)
x = float(split_line[1].strip())
xlist.append(x)
count = count + 1
return xlist
which I know works because I can
xstuff = xPoints()
print xstuff
I've also managed to get the second program, which right now just draws a y = x line into infinity:
class SubplotAnimation(animation.TimedAnimation):
def __init__(self):
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
self.t = np.linspace(0, 80, 400)
self.x = self.t*100
self.y = self.t*100
self.z = 10 * self.t
ax1.set_xlabel('x')
ax1.set_ylabel('y')
self.line1a = Line2D([], [], color='red', linewidth=2)
self.line1e = Line2D([], [], color='red', marker='o', markeredgecolor='r')
ax1.add_line(self.line1a)
ax1.add_line(self.line1e)
ax1.set_xlim(-1000, 1000)
ax1.set_ylim(-1000, 1000)
ax1.set_aspect('equal', 'datalim')
animation.TimedAnimation.__init__(self, fig, interval=50, blit=True)
def _draw_frame(self, framedata):
i = framedata
head = i - 1
head_len = 10
head_slice = (self.t > self.t[i] - 1.0) & (self.t < self.t[i])
self.line1a.set_data(self.x[head_slice], self.y[head_slice])
self.line1e.set_data(self.x[head], self.y[head])
self._drawn_artists = [self.line1a, self.line1e]
def new_frame_seq(self):
return iter(range(self.t.size))
ani = SubplotAnimation()
plt.show()
So I pasted the first code right above the second code and then tried to change this
self.t = np.linspace(0, 80, 400)
self.x = self.t*100
self.y = self.t*100
to this:
self.t = tPoints()
self.x = xPoints()
self.y = yPoints()
but when I do this, I get errors
If I put the def xPoints(): into a class points(): , then try to print points(), it prints "<__main__.points instance at 0x0399F148>"
If I don't put def xPoints(): into a class, I get this error:
Traceback (most recent call last):
etc etc
File "program.py", Line 76, in _draw_frame
self.line1a.set_data(self.x[head_slice], self.y[head_slice])
TypeError: only integer arrays with one element can be converted to an index.
So I'm pretty much stuck at this point. I appreciate that its kind of a long shot asking for help, but it seems like such a simple program that someone would be able to answer it without breaking a sweat. I really appreciate any help on this! Let me know if I can provide any additional information.
Sincerely,
Nick