1 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
為了使您的示例起作用,您必須更改兩件事:
從某處存儲(chǔ)返回值
FuncAnimation
。否則你的動(dòng)畫(huà)會(huì)在plt.show()
.如果不想畫(huà)線而只想畫(huà)點(diǎn),請(qǐng)
plt.plot
使用animation
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.set_xlim(0,2)
ax.set_ylim(0,2)
line, = plt.plot(0,0,'bo')
def animation(i):
x=np.linspace(0,2,100)
y=np.linspace(0,1,100)
plt.plot(x[i],y[i],'bo')
return line,
my_animation=FuncAnimation(fig, animation, frames=np.arange(100),interval=10)
plt.show()
如果你只想在圖表上有一個(gè)移動(dòng)點(diǎn),你必須設(shè)置并從inblit=True返回結(jié)果:plot.plotanimation
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.set_xlim(0,2)
ax.set_ylim(0,2)
line, = plt.plot(0,0,'bo')
def animation(i):
x=np.linspace(0,2,100)
y=np.linspace(0,1,100)
return plt.plot(x[i],y[i],'bo')
my_animation=FuncAnimation(
fig,
animation,
frames=np.arange(100),
interval=10,
blit=True
)
plt.show()
此外,您可能想擺脫 (0,0) 處的點(diǎn),并且不想為每個(gè)動(dòng)畫(huà)幀計(jì)算xand :y
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.set_xlim(0,2)
ax.set_ylim(0,2)
x=np.linspace(0,2,100)
y=np.linspace(0,1,100)
def animation(i):
return plt.plot(x[i], y[i], 'bo')
my_animation=FuncAnimation(
fig,
animation,
frames=np.arange(100),
interval=10,
blit=True
)
plt.show()
添加回答
舉報(bào)