【问题标题】:Why is a point not moving when plotting using FuncAnimation?为什么使用 FuncAnimation 绘图时点不动?
【发布时间】:2020-07-23 19:15:51
【问题描述】:

我正在尝试从头开始模拟双星系统中行星的运动。为此,我需要能够在动画图中绘制点。在对整个事情进行编码之前,我正在学习使用 pyplot 为情节制作动画。到目前为止,我还没有为移动点设置动画的运气。在查看了几个教程和文档之后,我得到了:

import matplotlib
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)
    line.set_data(x[i],y[i],'bo')
    return line,
FuncAnimation(fig, animation, frames=np.arange(100),interval=10)
plt.show()

但是这段代码的输出只是 0,0 的一个点,我不明白我可能做错了什么。

【问题讨论】:

    标签: python python-3.x matplotlib matplotlib-animation


    【解决方案1】:

    要使您的示例正常工作,您必须更改两件事:

    1. FuncAnimation 的返回值存储在某处。否则你的动画会在plt.show()之前被删除。
    2. 如果不想画线而只想画点,请在animation 中使用plt.plot
    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()
    

    如果你只想在图形上有一个移动点,你必须设置blit=True并在animation中返回plot.plot的结果:

    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) 处的点,并且您不想为每个动画帧计算 xy

    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()
    

    【讨论】:

    • 我已经用blit=True 测试了代码,但它没有任何改变——结果图上仍然有一系列点。
    • 我的 blit=True 示例适用于 Python 3.8.3、IPython 7.16.1、matplotlib 3.2.1。您是否尝试过按原样运行它们?
    • 是的,但是要在 Jupyter Notebook 中查看动画,我总是在脚本顶部添加 %matplotlib notebook。对我有用的方法是stackoverflow.com/questions/35521545/… 的公认答案
    • 很高兴您找到了答案。
    • 是的,但是使用 blit 的解决方案似乎更紧凑。我的版本:(Python 3.7.7 IPython 7.13.0 matplotlib 3.1.3)我很好奇它是否适用于@JulioHC
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2020-11-06
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多