import matplotlib.pyplot as plt
import matplotlib.animation as mpla
import numpy as np
with open('../y_out.txt', 'r') as file:
data = file.readlines()
t = []
x = []
y = []
kx = []
ky = []
for i, line in enumerate(data):
if i == 0: continue
values = line.strip().split()
t.append(float(values[0]))
x.append(float(values[1]))
y.append(float(values[2]))
kx.append(float(values[3]))
ky.append(float(values[4]))
fig, ax = plt.subplots()
animating = True
ax.quiver(x, y, kx, ky, color='red')
if not animating:
ax.plot(x, y, label='x(t), y(t)', linewidth=2)
ax.scatter(x, y)
else:
line, = ax.plot([], [], label="x(t),y(t)")
scatter = ax.scatter([], [])
def animate(i):
line.set_data(x[:i],y[:i])
scatter.set_offsets(np.column_stack((x[:i], y[:i])))
return line, scatter
anim = mpla.FuncAnimation(
plt.gcf(), animate, interval=100, frames=len(t)+1, repeat=True, repeat_delay=500,
)
ax.set_xlim(min(x) - 1, max(x) + 1)
ax.set_ylim(min(y) - 1, max(y) + 1)
ax.set_xlabel('x(t)')
ax.set_ylabel('y(t)')
ax.set_title('Graph of x(t) and y(t)')
ax.legend()
plt.minorticks_on()
plt.show()