Charts and maps

Streamlit · Essentials · 10 min

Visual results are the heart of most data apps, and Streamlit renders the libraries you already know.

Streamlit has quick built-in charts that take a DataFrame and need no plotting library code:

  • st.line_chart(df, x="date", y=["a", "b"])
  • st.area_chart, st.bar_chart (with horizontal=True or stack=False), st.scatter_chart
  • st.map(df) plots points from lat/lon (or latitude/longitude) columns on a map

They are built on Altair and are interactive (hover, zoom), but offer limited customization.

For full control, render a figure from your plotting library:

  • st.plotly_chart(fig): interactive Plotly figures; with on_select="rerun" it returns the user's selected points.
  • st.altair_chart(chart) and st.vega_lite_chart.
  • st.pyplot(fig): Matplotlib and seaborn figures as static images. Pass the figure object explicitly instead of relying on global pyplot state, which is shared between sessions.
  • st.pydeck_chart for advanced 3D maps, st.graphviz_chart for graphs.

Charts stretch to the container width by default; use width= and height= to size them, and put them inside columns or tabs for layout.

Because the script reruns on each interaction, chart code reruns too. Build figures from cached data so redrawing stays fast, and avoid generating heavy Matplotlib figures for every keystroke in a text input.

Example

import numpy as np
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import streamlit as st

rng = np.random.default_rng(0)
df = pd.DataFrame({"day": pd.date_range("2026-01-01", periods=60),
                   "visits": rng.poisson(200, 60).cumsum(),
                   "signups": rng.poisson(20, 60).cumsum()})

st.line_chart(df, x="day", y=["visits", "signups"])        # built-in

fig = px.scatter(df, x="visits", y="signups", title="Visits vs signups")
st.plotly_chart(fig)                                       # interactive

mfig, ax = plt.subplots()
ax.hist(np.diff(df.visits), bins=15)
st.pyplot(mfig)                                            # static image

pts = pd.DataFrame({"lat": [59.91, 48.86], "lon": [10.75, 2.35]})
st.map(pts, zoom=3)

Remember

  • Built-in st.line_chart, bar_chart, scatter_chart and map take DataFrames directly
  • st.plotly_chart, st.altair_chart and st.pyplot render library figures
  • Pass Matplotlib figure objects explicitly to st.pyplot

Watch out for

  • Calling st.pyplot() with no figure, relying on pyplot's global state shared across users.

Check yourself

  1. You need hover tooltips, zoom and the ability to read which points a user lasso-selected. Which element fits?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.streamlit.io/develop/api-reference/charts

Builds on

Unlocks

Nothing builds on this yet.