[英]Plot an animated line between two points (one is moving) in python
這里有一個固定點和一個可變點,它們每次迭代都會更改其位置。 我想在每次迭代中為這兩個點之間的一條線設置動畫,好像有一條線在改變他的漸變。
這是這兩點的代碼:
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()
謝謝!
動畫線可以取2個點,而不是一個點,這樣,兩個點都用一條線連接。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.