st.plotly_chart renders a Plotly figure inside a Streamlit app. Create the figure first, then pass it to the function; the result is an interactive visualization rather than a static image. section 6.5.1 section 6.5.2
Use the element to:
- Display time-series figures with lines, markers, titles, and labeled axes. section 6.5.1
- Give users built-in zooming, hover tooltips, full-screen viewing, and image download. section 6.5.1
- Render several metric charts in tabs or place chart components side-by-side with columns. section 6.5.1 section 6.5.2
- Show charts based on a selected grouping dimension, such as gender, state, category, or product. section 6.5.2
Render a chart across its container
chart = get_time_series_chart(df, metric) st.plotly_chart(chart, use_container_width=True) The first line produces a Plotly figure from prepared data. The second renders it and expands it to the containing box, avoiding oversized charts or unnecessary whitespace. The same pattern renders pie charts. section 6.5.1 section 6.5.2
import plotly.graph_objs as go
import streamlit as st
from data_wrangling import get_metric_time_series
from metric_config import metrics, display_metrics
...
def time_series_chart(df):
with st.container(border=True):
chart_tabs = st.tabs(display_metrics)
for idx, met in enumerate(display_metrics):
with chart_tabs[idx]:
chart = get_time_series_chart(df, metrics[met])
st.plotly_chart(chart, use_container_width=True)
A complete dashboard pattern combines st.plotly_chart with tabs: build a chart for each metric, select the matching tab, and render every figure during the initial load. section 6.5.1
