# How to animate a line chart

**URL:** <https://discuss.streamlit.io/t/how-to-animate-a-line-chart/164>\
**Category:** Using Streamlit\
**Tags:** matplotlib\
**Created:** [October 4, 2019, 1:16pm UTC](https://discuss.streamlit.io/t/how-to-animate-a-line-chart/164 "2019-10-04T13:16:45Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Adrien\_Treuille](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/adrien_treuille/32/8_2.png) [@Adrien\_Treuille](https://discuss.streamlit.io/u/Adrien_Treuille)\
**Post date:** [October 4, 2019, 4:58pm UTC](https://discuss.streamlit.io/t/how-to-animate-a-line-chart/164/2 "2019-10-04T16:58:41Z")

</div>

Hi @cristian.cotoi! Welcome to the Streamlit community! 🤗

It is absolutely possible to animate matplotlib charts in Streamlit. I’ve modified your code in [this gist](https://gist.github.com/treuille/4f70bff85ec5856cc7696f5c73f1ea11):

```python
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)

```

You can run this code right now by running:

```bash
streamlit run https://gist.githubusercontent.com/treuille/4f70bff85ec5856cc7696f5c73f1ea11/raw

```

Please note that if you use [Altair](https://altair-viz.github.io/), Streamlit also gives you [more powerful and efficient constructs to animate charts](https://streamlit.io/docs/search.html?q=add_rows&check_keywords=yes&area=default) like `add_rows`.

To see an example of this more advanced usage (including code snippet), please run

```bash
streamlit hello

```

and choose _Plotting Demo_ from the select-box in the sidebar.

---

_[View the full topic](https://discuss.streamlit.io/t/how-to-animate-a-line-chart/164)._
