Matplotlib动画未显示在PyCharm中

时间:2018-06-19 12:41:53

标签: python matplotlib pycharm

尝试执行此代码:

"""
A simple example of an animated plot
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))


def animate(i):
    line.set_ydata(np.sin(x + i/10.0))  # update the data
    return line,


# Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
                              interval=25, blit=True)
plt.show()

src:https://matplotlib.org/examples/animation/simple_anim.html

在PyCharm中显示:

enter image description here

但是情节并没有作为动画执行。

如何将此图作为动画执行?我需要为此python代码编辑PyCharm配置吗? enter image description here

1 个答案:

答案 0 :(得分:1)

这段代码成功了:

"""
A simple example of an animated plot
"""


    import matplotlib; matplotlib.use("TkAgg")
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation


    fig, ax = plt.subplots()

    x = np.arange(0, 2*np.pi, 0.01)
    line, = ax.plot(x, np.sin(x))


    def animate(i):
        line.set_ydata(np.sin(x + i/10.0))  # update the data
        return line,


    # Init only required for blitting to give a clean slate.
    def init():
        line.set_ydata(np.ma.array(x, mask=True))
        return line,

    ani = animation.FuncAnimation(fig, animate, np.arange(1, 20000), init_func=init,
                                  interval=25, blit=True)
    plt.show()

注意添加import matplotlib; matplotlib.use("TkAgg")

此问题/答案也有所帮助:Matplotlib animations do not work in PyCharm