Reputation: 23
Here there´s a fixed point and a variable point, which changes his position every iteration. I would like to animate a line between these two points every iteration, as if there was a line changing his gradient.
Here is the code of these two points:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
list_var_points = (1, 5, 4, 9, 8, 2, 6, 5, 2, 1, 9, 7, 10)
fig, ax = plt.subplots()
xfixdata, yfixdata = [14], [8]
xdata, ydata = [5], []
ln, = plt.plot([], [], 'ro', animated=True)
plt.plot([xfixdata], [yfixdata], 'bo')
def init():
ax.set_xlim(0, 15)
ax.set_ylim(0, 15)
return ln,
def update(frame):
ydata = list_var_points[frame]
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=range(len(list_var_points)),
init_func=init, blit=True)
plt.show()
Thank you!
Upvotes: 1
Views: 4792
Reputation: 339062
The animated line can take 2 points instead of only one, such that both points are connected with a line.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
list_var_points = (1, 5, 4, 9, 8, 2, 6, 5, 2, 1, 9, 7, 10)
fig, ax = plt.subplots()
xfixdata, yfixdata = 14, 8
xdata, ydata = 5, None
ln, = plt.plot([], [], 'ro-', animated=True)
plt.plot([xfixdata], [yfixdata], 'bo', ms=10)
def init():
ax.set_xlim(0, 15)
ax.set_ylim(0, 15)
return ln,
def update(frame):
ydata = list_var_points[frame]
ln.set_data([xfixdata,xdata], [yfixdata,ydata])
return ln,
ani = FuncAnimation(fig, update, frames=range(len(list_var_points)),
init_func=init, blit=True)
plt.show()
Upvotes: 3