Skip to content

Instantly share code, notes, and snippets.

@treuille
Created October 4, 2019 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treuille/4f70bff85ec5856cc7696f5c73f1ea11 to your computer and use it in GitHub Desktop.
Save treuille/4f70bff85ec5856cc7696f5c73f1ea11 to your computer and use it in GitHub Desktop.
Answer: Animating a Matplotlib chart
import matplotlib.pyplot as plt
import numpy as np
import streamlit as st
import time
fig, ax = plt.subplots()
max_x = 5
max_rand = 10
x = np.arange(0, max_x)
ax.set_ylim(0, max_rand)
line, = ax.plot(x, np.random.randint(0, max_rand, max_x))
the_plot = st.pyplot(plt)
def init(): # give a clean slate to start
line.set_ydata([np.nan] * len(x))
def animate(i): # update the y values (every 1000ms)
line.set_ydata(np.random.randint(0, max_rand, max_x))
the_plot.pyplot(plt)
init()
for i in range(100):
animate(i)
time.sleep(0.1)
@treuille
Copy link
Author

treuille commented Oct 4, 2019

Created in response to this question.

@demmerichs
Copy link

With newer streamlit versions starting from 0.52.2 and upwards, you need to provide the clear_figure=False flag to both .pyplot(plt) calls. More on this in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment