import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Get the Figure
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(8, 8))

print(axes)
def animate(i):
    for ax in axes:
        ax.clear()
    ts = []
    xs = []
    ys = []
    vxs = []
    vys = []
    speeds = []

    # Open file.csv generated by Face.py
    graph_data = open('file.csv', 'r').read()
    lines = graph_data.split('\n')
    for line in lines[1:]:
        if len(line) > 1:  # Skip the first labels line in csv file
            t, x, y, vx, vy, speed = line.split(',')
            ts.append(float(t))
            xs.append(float(x))
            ys.append(float(y))
            vxs.append(float(vx))
            vys.append(float(vy))
            speeds.append(float(speed))
        
        # Lets add these lists xs, ys to the plot
        for ax in axes:
            ax.clear()
        axes[0].plot(ts, xs, '-o', color=(0, 1, 0.25))
        axes[0].set_xlabel("Time")
        axes[0].set_ylabel("X position")
        axes[0].set_title("Live x coordinate")
        
        axes[1].plot(ts, ys, '-o', color=(0, 0, 1))
        axes[1].set_xlabel("Time")
        axes[1].set_ylabel("Y position")
        axes[1].set_title("Live y coordinate")
         
        fig.tight_layout()  # To remove outside borders


# Lets call the animation function
ani = animation.FuncAnimation(fig, animate, interval=1)
plt.show()
