0

I got some sort of a problem with a pendulum animation, I tried to display my animation (the pendulum's movement) next to a graph in two separate axes, but when I try my code, it barely works displaying two axes that overlap on one another... Here is what I tried:

PS: best would be that the circles I was intended to add at the end of my pendulum appear on the final animation, but I really have no idea how to put them only on a particular ax

from numpy import sin, cos, pi, array
import numpy as np
import scipy.integrate 
import matplotlib.pyplot as plt
import matplotlib.animation as animation

g = 10
y0 = np.array([np.pi / 2.0, 0])  # angle, vitesse
j = 0.2

def f(y, t):
    return np.array([y[1], -g * np.sin(y[0])-j*y[1]]) 

t = np.linspace(0, 100, 10000)
y = scipy.integrate.odeint(f, y0, t)
theta, thetadot = y[:, 0], y[:, 1]

fig, axs = plt.subplots(1,2)

axs[0] = fig.add_subplot(xlim=(-1.5, 1.5), ylim=(-1.5, 1.5))
axs[0].grid()
axs[0].set_box_aspect(1)

# anchor = plt.Circle((0, 0), 0.01, color='black')
# mass = plt.Circle((sin(y0[0]),-cos(y0[0])), 0.2, color='black')
pendulums = axs[0].plot((0, sin(y0[0])), (0, -cos(y0[0])), 'o-', color = 'black')

# plt.gca().add_patch(weight)  # adding circles
# plt.gca().add_patch(attach)

phase = axs[1].plot(theta,thetadot)

def animate(i):
    angle = theta[i]
    x = (0, sin(angle))
    y = (0, -cos(angle))
    #mass.center = (x[1],y[1])
    pendulums[0].set_data(x, y) 

anim = animation.FuncAnimation(fig, animate, interval=10)

plt.show()

0 Answers0