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:
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!!
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
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.