This is one of those situations where trying to tie together info from the tutorials and advanced concepts is failing.
I’ve managed to create scatter sub-plots but now I want to make them live by continuously appending data. Scatter charts appear to only be available through matplotlib but the advanced concept uses the built in chart object so I’m struggling to figure out how to do it.
In general, to append data to any chart, you use the add_rows function. (You call add_rows on the value returned from the various st. calls.)
However, matplotlib charts aren’t “live” (they don’t live in the browser and therefore aren’t interactive; instead, matplotlib renders your chart to a static image which is then sent to the browser.) So add_rows does not work with matplotlib.
I’d recommend using Altair to render your scatter chart instead. (Streamlit has good support for Altair; it’s actually what st.line_chart, area_chart, and bar_chart all use under the hood.)
For example:
import time
import altair as alt
import numpy as np
import pandas as pd
import streamlit as st
# Generate some random data
df = pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"])
# Build a scatter chart using altair. I modified the example at
# https://altair-viz.github.io/gallery/scatter_tooltips.html
scatter_chart = st.altair_chart(
alt.Chart(df)
.mark_circle(size=60)
.encode(x='a', y='b', color='c')
.interactive()
)
# Append more random data to the chart using add_rows
for ii in range(0, 100):
df = pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"])
scatter_chart.add_rows(df)
# Sleep for a moment just for demonstration purposes, so that the new data
# animates in.
time.sleep(0.1)
Great example. I was actually not aware that you can use .add_rows on a st.altair_chart: I thought it was only on the built in streamlit charts. Can you do that on plotly charts as well?
Maybe this is worth making clearer in the documentation as this really expands the useability of Streamlit
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.