2 回答
TA貢獻2041條經驗 獲得超4個贊
回答
您可以用來ax.arrow繪制箭頭。
請注意,您應該在每次迭代時使用 清除繪圖ax.cla()并調整軸限制ax.set()。
代碼
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(12, 8))
ax.set(xlim=(0, 104), ylim=(0, 68))
x_start, y_start = (50, 35)
x_end, y_end = (90, 45)
N = 50
x = np.linspace(x_start, x_end, N)
y = np.linspace(y_start, y_end, N)
def animate(i):
? ? ax.cla()
? ? ax.arrow(x_start, y_start,
? ? ? ? ? ? ?x[i] - x_start, y[i] - y_start,
? ? ? ? ? ? ?head_width = 2, head_length = 2, fc = 'black', ec = 'black')
? ? ax.set(xlim = (0, 104), ylim = (0, 68))
ani = animation.FuncAnimation(fig, animate, interval=20, frames=N, blit=False, save_count=50)
plt.show()
TA貢獻1757條經驗 獲得超7個贊
您只需將線函數更改為箭頭函數即可。但請注意,您首先需要計算箭頭的終點,因為根據文檔,您只能指定長度 dx,dy。通過使用畢達哥拉斯,起點為x[0],y[0],為轉換后的終點。dxdy
我認為你現在可以自己解決這個問題。
添加回答
舉報
