to update altair chart with add_rows function: Appending to scatter chart sub-plots , but I am not able to remove past data to keep visible in the chart only a certain amount of samples (e.g. 1000 samples)
Would anybody be so kind to suggest me how to plot a stream of real-time data with streamlit?
Hi @napoles3d, thank you very much for your suggestion!!
Unfortunately the script that you linked is exploiting “add_rows” function that I have testes already.
This function is very convenient, but it has a couple of drawbacks:
Speed
There is no function to “remove the rows”. Thus the chart is constantly increasing in size and after some seconds it is impossible to read.
If you have some additional suggestion that would be very much appreciated!
Thank you again
So, I am not sure about the compatibility between Streamlit and Pyqtgraph (I will check about this later).
However, for your second problem with keeping visible only recent incoming data, you should be using the Deque data structure. When data appending to maximum (say 1000 sample) the first appended value will remove from the list (Time complexity is O(n) time). https://www.geeksforgeeks.org/deque-in-python/
import streamlit as st
import time
import matplotlib.pyplot as plt
import numpy as np
from collections import deque
fig, ax = plt.subplots()
max_samples = 100
max_x = max_samples
max_rand = 100
x = np.arange(0, max_x)
y = deque(np.zeros(max_samples), max_samples)
ax.set_ylim(0, max_rand)
line, = ax.plot(x, np.array(y))
the_plot = st.pyplot(plt)
def animate(): # update the y values (every 1000ms)
line.set_ydata(np.array(y))
the_plot.pyplot(plt)
y.append(np.random.randint(max_x)) #append y with a random integer between 0 to 100
for i in range(200):
animate()
time.sleep(0.01)
@Cly1st , again thank you very much for your message.
I have tested your code, but matplotlib is extremely slow. I reach an update rate of maximum 2Hz.
Not even 10Hz as per your code.
As I said, I have also tried to use add_rows. An example follows:
import streamlit as st
import time
import numpy as np
progress_bar = st.sidebar.progress(0)
status_text = st.sidebar.empty()
last_rows = np.random.randn(1, 1)
chart = st.line_chart(last_rows)
for i in range(1, 101):
new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0)
new_rows = np.random.randn(1, 1)
status_text.text("%i%% Complete" % i)
chart.add_rows(new_rows)
progress_bar.progress(i)
last_rows = new_rows
time.sleep(0.05)
progress_bar.empty()
# Streamlit widgets automatically run the script from top to bottom. Since
# this button is not connected to any other logic, it just causes a plain
# rerun.
st.button("Re-run")
The problem is that the cart becase very quickly unreadable because I cannot remove rows.
Do you think that there is any way to explout deque within the streamlit chart?
I have been using PyQtGraph for the past months with success.
However, the graphics of PyQtGraph is not very captivating.
Do you think there is any new python alternative for fast updates of charts? (100Hz)