1 回答

TA貢獻1815條經驗 獲得超13個贊
我添加了簡單的條件sc_2。
if i == len(x):
sc_2.set_offsets([x_end, y_end])
我測試的代碼:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
x_start, y_start = (0, 0)
x_end, y_end = (90, 90)
x_1, y_1 = 0, 0
x_2, y_2 = 90, 90
plt.xlim((0, 100))
plt.ylim((0,100))
x = np.linspace(x_1, x_2, 50)
y = np.linspace(y_1, y_2, 50)
sc_1 = ax.scatter([], [], color="green", zorder=4)
line, = ax.plot([], [], color="crimson", zorder=4)
sc_2 = ax.scatter([], [], color="gold", zorder=4)
def animate(i):
## plot scatter point
sc_1.set_offsets([x_start, y_start])
## plot line
line.set_data(x[:i], y[:i])
## plot scatter point
if i == len(x):
sc_2.set_offsets([x_end, y_end])
return sc_1, line, sc_2
ani = animation.FuncAnimation(
fig=fig, func=animate, interval=100, blit=True, save_count=50)
plt.show()
添加回答
舉報