如何使用 matplotlib 为复杂函数制作动画?

How to animate a complex function with matplotlib?

我想用函数((phi^n)-((-1/phi)^n))/(5^0.5)(比奈公式)做一个动画,n ∈ ℝ, 以便图形从实轴上的直线开始,然后转移到实际图形中。 我尝试添加

from matplotlib.animation import FuncAnimation
.
.
.
    def g(val):
        main_graph.set_ydata(imag(f(x))*val)
        return main_graph,
    animation = FuncAnimation(main_graph, func=g, frames=arange(0, 10, 0.1), interval=10)
plt.show

但是,它没有用,我不知道为什么我按照各种教程进行操作并且所有教程都有相同的结果(错误) 我也试过了

import matplotlib.animation as animation
.
.
.
def init():
    main_graph.set_ydata([np.nan] * len(real(f(x))))
    return main_graph,
def g(val):
    main_graph.set_ydata(imag(f(x))*val)
    return main_graph,
ani = animation.FuncAnimation(main_graph, g, init_func=init, interval=2, blit=True, save_count=50)

两种情况下的错误都是AttributeError: 'Line2D' object has no attribute 'canvas'
这是完整的代码

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from numpy import arange, real, imag
phi = (1+(5**0.5))/2
x = arange(0,5,0.01)
def f(x):
    return ((phi**(x+0j))-((-1/phi)**(x+0j)))/(5**0.5)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position(('data', 0.0))
ax.spines['bottom'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#labels for x and y axes
plt.xlabel('real')
plt.ylabel('imag')
plt.grid(alpha=.4,linestyle=':')
main_graph, = plt.plot(real(f(x)),imag(f(x)), label='((phi**(x+0j))-((-1/phi)**(x+0j)))/(5**0.5)')
plt.legend()
    def g(val):
        main_graph.set_ydata(imag(f(x))*val)
        return main_graph,
    animation = FuncAnimation(main_graph, func=g, frames=arange(0, 10, 0.1), interval=10)
plt.show()

要查看最终图表,请使用此代码

import matplotlib.pyplot as plt
from numpy import arange, real, imag
phi = (1+(5**0.5))/2
x = arange(0,5,0.01)
def f(x):
    return ((phi**(x+0j))-((-1/phi)**(x+0j)))/(5**0.5)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position(('data', 0.0))
ax.spines['bottom'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#labels for x and y axes
plt.xlabel('real')
plt.ylabel('imag')
plt.grid(alpha=.4,linestyle=':')
main_graph, = plt.plot(real(f(x)),imag(f(x)), label='((phi**(x+0j))-((-1/phi)**(x+0j)))/(5**0.5)')
plt.legend()
plt.show()

我改编了matplotlib动画的例子documentation。以下是通过设置 blit=False

修改代码以允许修改轴元素(在本例中为图例)的方式
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from numpy import arange, real, imag
phi = (1+(5**0.5))/2
x = arange(0,5,0.01)
def f(x):
    return ((phi**(x+0j))-((-1/phi)**(x+0j)))/(5**0.5)
fig = plt.figure()
main_graph, = plt.plot(real(f(x)),imag(f(x)), label='((phi**(x+0j))-((-1/phi)**(x+0j)))/(5**0.5)')
#labels for x and y axes
plt.xlabel('real')
plt.ylabel('imag')
plt.grid(alpha=.4,linestyle=':')
#plt.legend(loc=4)

def init():
    global legs
    ax = fig.add_subplot(1, 1, 1)
    ax.spines['left'].set_position(('data', 0.0))
    ax.spines['bottom'].set_position(('data', 0))
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.set_ylim(-4,4)
    legs=ax.legend(loc=4, prop={'size': 12})
    return main_graph, 
    
def g(val):
    main_graph.set_ydata(imag(f(x))*val)
    label = '((phi**(x+0j))-((-1/phi)**(x+0j)))/(5**0.5)x{}'.format(val)
    legs.texts[0].set_text(label)
    return main_graph, 
#Note that blit has been set to False, because axes elements are being modified
animation = FuncAnimation(fig, func=g,frames=arange(0, 10, 0.1), init_func=init,interval=10,blit=False)
animation.save('animation.gif', writer='imagemagick', fps=30)
plt.show()

动画效果如下: