Forum Archive

Animated graph with matplotlib.animation

epalzeo

Hello

i've code in python 3 to make animated graph with datas coming in realtime

for this, i use

ani = FuncAnimation(fig, update, frames=np.linspace(0,40, 4096),
                    init_func=init, blit=True)
plt.show()

with update function where i update the graph objects

it works well on my desktop

In pythonista it shows the first frame then exit like the animation loop is not implemented. It does the init function only

Do you have any clue on this issue ?

Thanks in advance :)

JonB

You might try with the backend_pythonista, however many features are not implemented (like blit).

Plt.show shows a plot to the console, but it is a static image.

For animating specific types of data, you will be better off going with a custom view, since vector ops are a lot faster than generating a whole image each frame.

Kephy

Hi:
Is not possible to animate graphs yet? I took some examples and they’re not working. I’m sharing one of them:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def data_gen():
    t = data_gen.t
    cnt = 0
    while cnt < 1000:
        cnt+=1
        t += 0.05
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
data_gen.t = 0

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 5)
ax.grid()
xdata, ydata = [], []
def run(data):
    # update the data
    t,y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
    repeat=False)
plt.show()
cvp

@Kephy I don't know anything about matplotlib but try this little script, perhaps it could help you to start.

from io import BytesIO
import numpy as np
import matplotlib.pyplot as plt
import ui

v = ui.ImageView()
v.frame = (0,0,400,400)
v.present('sheet')
a = np.array([])

fig, ax = plt.subplots()

for i in range(20):
    # update the data
    a = np.append(a,np.random.randn(10))
    plt.plot(a)
    b = BytesIO()
    plt.savefig(b)
    plt.close(fig) # free memory
    v.image = ui.Image.from_data(b.getvalue())
Kephy

Thanks. So grateful. ☺️

JonB

@Kephy see
https://GitHub.com/jsbain/pythonista_matplotlib_backports