2
我製作了一個matplotlib動畫,其中箭頭箭頭在頁面中移動。這不能以通常的方式實現(創建一個顫動對象並用動畫的每一幀更新它),因爲儘管有更新u,v組件的方法,但沒有用於更改x ,y位置的箭頭。因此,我正在爲每個幀創建一個新的Quiver對象。保存matplotlib動畫時未清除前面的幀
當我做一個plt.show()
並且在屏幕上繪製動畫時,這可以正常工作。箭頭在頁面上從左向右移動,當一個箭頭出現時,前一個箭頭消失,這正是我想要的。但是,當我保存爲gif或mp4時,以前的箭頭不會被清除,所以我最終會出現一整行箭頭。我怎樣才能解決這個問題?
我的代碼如下:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
n = 21
x = np.linspace(-1.0, 1.0, num=n)
def animate(i):
q = plt.quiver(x[i:i+1], [0], [1], [0])
return q,
plt.gca().set_xlim([-1, 1])
anim = matplotlib.animation.FuncAnimation(plt.gcf(), animate, frames=n,
repeat=True, blit=True)
plt.show()
#anim.save('anim.gif', dpi=80, writer='imagemagick')
#anim.save('anim.mp4', dpi=80, writer='ffmpeg')
是否可以將您的設置用於透明圖形修補程序?你可以通過'matplotlib.rcParams ['savefig.frameon']'檢查,這應該是'True',以使每個新的補丁具有堅實的背景(http://matplotlib.org/api/pyplot_api.html)或設置參數使用'anim.save('anim.mp4',[..等等],extra_args = {'frameon':True})'(http://matplotlib.org/api/animation_api.html) – slushy
檢查此改變箭頭的位置:http://stackoverflow.com/questions/17758942/is-it-possible-to-update-matplotlib-quiver-position-coordinates-in-an-animation –
按建議使用q.set_offsets由@Jean完美的作品。謝謝! –