MatplotLib Animation: Decay example with streamlit, nothing plotted

I have enclosed a code, which is basically the decay example of Matplotlib animation class, which is supposed to plot an animating sine wave, decaying, which runs fine when running the script from Spyder, but then when comment out plt.show() and try running it with steamlit, the axes show on the page, but no plot is being drawn.

Code

import itertools

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

drawing1= st.empty()

def data_gen():
    for cnt in itertools.count():
        t = cnt / 10
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
        #the_plot.pyplot(plt)


def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 10)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []


#the_plot = st.pyplot(plt)


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)
    #the_plot.pyplot(plt)
    #the_plot.write(ax)
    #the_plot.write(fig)
    #the_plot.write(plt)
    
    drawing1.pyplot(plt)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init)
#the_plot.pyplot(plt)  
plt.show()
#the_plot.pyplot(plt)

Hi. I have asked this question a while back, however, not answered yet. Wonder if anyone can suggest, maybe I need to change the question or perhaps ask it elsewhere, to get an answer? Thanks

Hi @AustinFW, first welcome to the community. Sorry you didn’t get an answer. I think that this has been addressed before by @andfanilo in this post:

1 Like

So, I modified you code, just adding the import of streamlit.components.v1 and a line at the end for calling components.html to render the animation and it works!

here is the code:


import itertools

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import streamlit as st
import streamlit.components.v1 as components

drawing1= st.empty()

def data_gen():
    for cnt in itertools.count():
        t = cnt / 10
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
        #the_plot.pyplot(plt)


def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 10)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []


#the_plot = st.pyplot(plt)


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)
    #the_plot.pyplot(plt)
    #the_plot.write(ax)
    #the_plot.write(fig)
    #the_plot.write(plt)
    
    drawing1.pyplot(plt)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init)
#the_plot.pyplot(plt)  
plt.show()
#the_plot.pyplot(plt)
components.html(ani.to_jshtml(), height=1000)

Btw, plt.show() is not going to work. On Streamlit you have st.pyplot(fig) instead
Cheers!!

1 Like

Thanks a lot @napoles3d for this. It is working for the decay example, slightly slower than when you just run the script in Python. But the idea of using component.html is interesting for sure. I noticed as the post by @andfanilo suggests, it breaks for longer animations. I am going to look further into the Streamlit component feature to see how that may be resolved. Thanks again :slight_smile:

1 Like