Python 动画李萨如曲线不可见

Python 动画李萨如曲线不可见,python,matplotlib,animation,Python,Matplotlib,Animation,我对python动画非常陌生,所以请耐心听我说。 所以我试着让这条李萨如曲线动画化,就像上面的一样 如果需要,我有李萨如曲线的代码。我认为通过将pi/2(在代码中它是f)变小变大可以复制它,但图形没有出现。先谢谢你 尝试: #导入我们的模块 将matplotlib.pyplot作为plt导入 将numpy作为np导入 从matplotlib.animation导入FuncAnimation a=1 A=1 B=1 b=3 c=1 D=1 图=plt.图() f=3.14/2 而f>-3.14/2

我对python动画非常陌生,所以请耐心听我说。 所以我试着让这条李萨如曲线动画化,就像上面的一样

如果需要,我有李萨如曲线的代码。我认为通过将
pi/2
(在代码中它是
f
)变小变大可以复制它,但图形没有出现。先谢谢你

尝试:

#导入我们的模块
将matplotlib.pyplot作为plt导入
将numpy作为np导入
从matplotlib.animation导入FuncAnimation
a=1
A=1
B=1
b=3
c=1
D=1
图=plt.图()
f=3.14/2
而f>-3.14/2:
f-=1
扩展数据,ydata=[],[]
ax=plt.gca()
直线,=ax.plot([],[],lw=2)
def init():
行。设置_数据([],[])
回程线,
定义动画(i):
t=0.1*i
x=A*np.sin(A*t+f)+c
y=B*np.sin(B*t)+D
扩展数据追加(x)
ydata.append(y)
行。设置_数据(扩展数据、ydata)
#ax.set_facecolor('xkcd:black')
回程线,
anim=FuncAnimation(图,animate,init_func=init,frames=200,interval=20,blit=True)
anim.save('abclogo.gif',writer='pillow')

必须包括轴限制

ax = plt.gca()
ax.set_xlim(0,2)
ax.set_ylim(0,2)
line, = ax.plot([], [], lw=2)
或者,通过执行以下操作,在动画函数中不使用append,这会稍微有效一些:

# Import our modules 
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
a= 1
A = 1
B = 1
b = 3
c = 1
D = 1

f=3.14/2
while f > -3.14/2:
    f-=1

seq=np.arange(0,200,1)
x = A*np.sin(a*0.1*seq+f) + c
y = B*np.sin(b*0.1*seq) + D
fig, ax = plt.subplots() 
line, = ax.plot(x, y, color='k')

def animate(i):
    line.set_data(x[:i], y[:i])
    return line,

anim = FuncAnimation(fig, animate, frames=len(x),interval=25, blit=True)
anim.save('abclogo.gif', writer='imagemagick')

plt.show()

编辑2:

FuncAnimation
不提供很多控制。例如,您将无法访问轴元素并修改它们。通过使用for循环可以实现更好的控制,如下所示:

import numpy as np
import matplotlib.pyplot as plt
import math
###initializing the parameters##################
M=1
N=2
########setup the plot################
fig, ax = plt.subplots()
t = np.arange(0, 1000, 1)
x = np.sin(M*0.1*t) 
y = np.sin(N*0.1*t+math.pi/2.0)
ax.set_xlim(-1.25,1.25)
ax.set_ylim(-1.25,1.25)
##################

for i in t:
    phi=np.arange(0,10*math.pi,math.pi/50.)
    #phase shifting to give the impression of rotation
    y = np.sin(N*0.1*t+phi[i])
    line, = ax.plot(x[:i],y[:i],c='black')
    plt.pause(0.01)
    #remove the track
    line.remove()
    del line

将显示动画

您必须包括轴限制

ax = plt.gca()
ax.set_xlim(0,2)
ax.set_ylim(0,2)
line, = ax.plot([], [], lw=2)
或者,通过执行以下操作,在动画函数中不使用append,这会稍微有效一些:

# Import our modules 
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
a= 1
A = 1
B = 1
b = 3
c = 1
D = 1

f=3.14/2
while f > -3.14/2:
    f-=1

seq=np.arange(0,200,1)
x = A*np.sin(a*0.1*seq+f) + c
y = B*np.sin(b*0.1*seq) + D
fig, ax = plt.subplots() 
line, = ax.plot(x, y, color='k')

def animate(i):
    line.set_data(x[:i], y[:i])
    return line,

anim = FuncAnimation(fig, animate, frames=len(x),interval=25, blit=True)
anim.save('abclogo.gif', writer='imagemagick')

plt.show()

编辑2:

FuncAnimation
不提供很多控制。例如,您将无法访问轴元素并修改它们。通过使用for循环可以实现更好的控制,如下所示:

import numpy as np
import matplotlib.pyplot as plt
import math
###initializing the parameters##################
M=1
N=2
########setup the plot################
fig, ax = plt.subplots()
t = np.arange(0, 1000, 1)
x = np.sin(M*0.1*t) 
y = np.sin(N*0.1*t+math.pi/2.0)
ax.set_xlim(-1.25,1.25)
ax.set_ylim(-1.25,1.25)
##################

for i in t:
    phi=np.arange(0,10*math.pi,math.pi/50.)
    #phase shifting to give the impression of rotation
    y = np.sin(N*0.1*t+phi[i])
    line, = ax.plot(x[:i],y[:i],c='black')
    plt.pause(0.01)
    #remove the track
    line.remove()
    del line

动画显示了

我想你没有看到这个网站。动画看起来像是旋转的图形。我想你没有看到这个网站。动画应该看起来像是图形在旋转。