Updating position of a Mathplotlib ConnectionPatch

Viewed 52

I am trying to animate a Pendulum. Everything except the string for which I am using a ConnectionPatch is working.

L = 3
T = 3.47
phi0 = 10
omega = 2*np.pi/T
t = np.linspace(0, 2*T, 500)
phi = np.deg2rad(phi0) * np.cos(omega * t)

r = np.array((L * np.sin(phi), - L * np.cos(phi)))
fig, ax = plt.subplots()
ax.plot(*r)
ax.set_aspect("equal")
ax.grid()
ax.set_xlim(-1, 1)
ax.set_ylim(-4, 1)

point, = ax.plot(*r[:, 0], "ro")
con = mpl.patches.ConnectionPatch((0, 0), r[:, 0], "data", "data", fc="black",)
ax.add_artist(con)


def update(n):
    point.set_data(*r[:, n])
    con.set_positions(np.array((0, 0)), r[:, n],)
    return point, con,


ani=mpl.animation.FuncAnimation(fig, update, interval=5, frames=t.size, blit=1) 

I tracked the Problem down to the method con.set_positions(np.array((0, 0)), r[:, n],) not working properly. It does not update the positions and I don't understand why it does not work.

Thank you for your Help.

1 Answers

You need to set the position to con.xy1 and con.xy2, see source:

def update(n):
    point.set_data(*r[:, n])
    con.xy1 = np.array((0, 0))
    con.xy2 = r[:, n]
    return point, con,

enter image description here

Related