Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib animation not displaying in PyCharm

Attempting to execute this code :

"""
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

displays in PyCharm :

enter image description here

But the plot is not executed as an animation.

How to execute this plot as an animation ? Do i need to edit the PyCharm configuration for this python code ? enter image description here

like image 931
blue-sky Avatar asked Jun 19 '18 12:06

blue-sky


1 Answers

This code did the trick :

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

Note addition of import matplotlib; matplotlib.use("TkAgg")

Also, this question/answer helped somewhat : Matplotlib animations do not work in PyCharm

like image 56
blue-sky Avatar answered Sep 19 '22 00:09

blue-sky