我有兩個資料集y1 = vol1,范圍y2 = vol2相同x(0 到 5000,步長為 10)。我想使用函式影片來首先制作影片y1,然后再制作影片,同時保留y2圖形。y1這是我從幾個例子中得到的(包括this):
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
x = range(0, 5000, 10)
y1 = vol1
y2 = vol2
fig, ax = plt.subplots()
ax.set_xlim(0, 5000)
ax.set_ylim(0, 1000)
l1, = plt.plot([],[],'b-')
l2, = plt.plot([],[],'r-')
def init1():
return l1,
def init2():
return l2,
def animate1(i):
l1.set_data(x[:i],y1[:i])
return l1,
def animate2(i):
l2.set_data(x[:i-500],y2[:i-500])
return l2,
def gen1():
i = 0
while(i<500):
yield i
i = 1
def gen2():
j = 500
while(j<1000):
yield j
j = 1
ani1 = FuncAnimation(fig, animate1, gen1, interval=1, save_count=len(x),
init_func=init1, blit=True,
repeat=False)
ani2 = FuncAnimation(fig, animate2, gen2, interval=1, save_count=len(x),
init_func=init2, blit=True,
repeat=False)
# ani.save('ani.mp4')
plt.show()
我的想法是制作兩個“計數器” gen1,gen2但由于兩個資料集的 x 值相同,我試圖在animate2函式中對此進行補償。但這不起作用..顯然,我對python很陌生,我很感激任何幫助。
uj5u.com熱心網友回復:
我只會做一個影片,跟蹤幀的行長:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation, FFMpegWriter
x = np.linspace(0, 2 * np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
k = 0
fig, ax = plt.subplots()
ax.set_xlim(0, x.max())
ax.set_ylim(-1.5, 1.5)
l1, = plt.plot([],[],'b-')
l2, = plt.plot([],[],'r-')
def animate1(i):
global k
if k > 2 * len(x):
# reset if "repeat=True"
k = 0
if k <= len(x):
l1.set_data(x[:k],y1[:k])
else:
l2.set_data(x[:k - len(x)],y2[:k - len(x)])
k = 1
ani1 = FuncAnimation(fig, animate1, frames=2*len(x), interval=1, repeat=True)
writer = FFMpegWriter(fps=10)
ani1.save("test.mp4", writer=writer)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480628.html
標籤:Python 功能 matplotlib 动画片
上一篇:從資料框中繪制每個條形圖的值
下一篇:Python從串列中創建3d圖
