Charts in Streamlit: st.line_chart vs Matplotlib vs Plotly vs Altair

Streamlit can draw charts four main ways. Here is the same chart in st.line_chart, st.pyplot, st.plotly_chart, and st.altair_chart, and when to pick each.

Streamlit draws a chart with one call, and which call you use decides how much control you get. st.line_chart (and st.bar_chart, st.scatter_chart, st.area_chart) take a DataFrame and need no other library. For anything more custom, build the figure in Matplotlib, Plotly, or Altair and hand it to st.pyplot, st.plotly_chart, or st.altair_chart. This post draws the same chart four ways so you can compare the code and the result. It targets Streamlit 1.67.

Quick Example

Save this as app.py and run streamlit run app.py. It has 24 months of simulated active users on three plans and draws them with the built-in line chart.

import numpy as np
import pandas as pd
import streamlit as st

rng = np.random.default_rng(8)
months = pd.date_range("2024-01-01", periods=24, freq="MS")
df = pd.DataFrame({
    "month": months,
    "Free": (4000 * 1.03 ** np.arange(24) + rng.normal(0, 150, 24)).round(),
    "Pro": (900 * 1.06 ** np.arange(24) + rng.normal(0, 60, 24)).round(),
    "Team": (250 * 1.09 ** np.arange(24) + rng.normal(0, 25, 24)).round(),
})
colors = ["#6741d9", "#f08c00", "#12b886"]

st.line_chart(df, x="month", y=["Free", "Pro", "Team"], color=colors,
              y_label="Monthly active users")

Streamlit built-in line chart of monthly active users for the Free, Pro and Team plans, all trending upward

x and y pick columns, color takes a list of hex colors (one per series), and x_label / y_label rename the axes. The built-in charts are interactive Vega-Lite charts: hover for tooltips, and each has a fullscreen button. The rest of this post builds on df and colors from this block.

The Same Chart in Matplotlib, Plotly, and Altair

Each library gets the same data and colors. The only Streamlit-specific part is the last line of each block.

Matplotlib with st.pyplot

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(7, 3.5))
for plan, c in zip(["Free", "Pro", "Team"], colors):
    ax.plot(df["month"], df[plan], label=plan, color=c)
ax.set_ylabel("Monthly active users")
ax.legend(frameon=False)
st.pyplot(fig)

st.pyplot renders a static image. You get all of Matplotlib's control over layout, annotations, and styling, but no hover or zoom. Create a new fig on every run of the script rather than using the global plt state, because Streamlit reruns the whole script on each interaction. See adding horizontal and vertical lines for the kind of annotation this is good at.

Plotly with st.plotly_chart

import plotly.express as px

long = df.melt("month", var_name="plan", value_name="users")
fig = px.line(long, x="month", y="users", color="plan",
              color_discrete_sequence=colors,
              labels={"users": "Monthly active users"})
st.plotly_chart(fig, width="stretch")

Plotly gives you the richest interaction: hover, zoom, pan, legend toggling, and selection events. Use width="stretch" to fill the column. The older use_container_width=True still works in 1.67 but is marked deprecated in the docstring. Plotly Express wants long-format data, so melt first. Streamlit applies its own theme by default, and theme=None turns that off if you want Plotly's look.

Altair with st.altair_chart

import altair as alt

chart = alt.Chart(long).mark_line().encode(
    x="month:T",
    y=alt.Y("users:Q", title="Monthly active users"),
    color=alt.Color("plan:N", scale=alt.Scale(range=colors)),
    tooltip=["month:T", "plan:N", "users:Q"],
)
st.altair_chart(chart, width="stretch")

Altair is what Streamlit's own charts use under the hood, so the result looks the closest to st.line_chart. It also takes long-format data. See an introduction to Altair if the encoding syntax is new.

Side by side

Four screenshots of the same three-line chart rendered by st.line_chart, st.pyplot, st.plotly_chart and st.altair_chart

The data and colors are identical, and the differences are in the defaults. Matplotlib draws a boxed static image with its own fonts. The other three use Streamlit's font and light grid lines. The built-in chart puts the legend below the plot and labels the x-axis by month names; Plotly and Altair put the legend on the right and name the color field plan.

Which Streamlit Chart Call Should You Use?

Call Best for Interactive Extra install
st.line_chart, st.bar_chart, st.area_chart, st.scatter_chart Quick charts straight from a DataFrame Hover tooltips None
st.pyplot Anything Matplotlib or Seaborn can draw; precise static layouts No matplotlib
st.plotly_chart Rich interactivity, many chart types, selection events Yes plotly
st.altair_chart Declarative charts, linked brushing and selections Yes altair

Start with the built-in call. Move to Plotly or Altair when you need something it can't express, such as dual axes, annotations, or a chart type it doesn't have. Use st.pyplot when the chart already exists as Matplotlib or Seaborn code.

Bar and Scatter Charts

The built-in family covers the common cases. st.bar_chart can stack bars, and sort=False keeps them in data order:

last = df.tail(6).assign(month=lambda d: d["month"].dt.strftime("%b %Y"))
st.bar_chart(last, x="month", y=["Free", "Pro", "Team"], color=colors,
             stack=True, sort=False, y_label="Monthly active users")

Stacked bar chart of monthly active users for the last six months, July to December 2025, growing each month

Without sort=False, Streamlit sorts the string months alphabetically, so August comes before July. Passing real dates instead of strings avoids this, but then the axis labels are dates rather than "Jul 2025".

st.scatter_chart takes a column name for color and groups the points by it:

accounts = pd.DataFrame({
    "sessions": rng.gamma(5, 6, 150),
    "plan": rng.choice(["Pro", "Team"], 150, p=[0.7, 0.3]),
})
accounts["revenue"] = accounts["sessions"] * np.where(accounts["plan"] == "Team", 9, 4) \
    + rng.normal(0, 25, 150)
st.scatter_chart(accounts, x="sessions", y="revenue", color="plan",
                 y_label="Monthly revenue ($)")

Scatter chart of sessions versus monthly revenue for 150 accounts, with Team plan points rising faster than Pro plan points

The Team accounts climb more than twice as steeply as the Pro accounts (the data uses $9 and $4 of revenue per session). Colors are assigned automatically from a column, so two similar blues can be hard to tell apart on a projector. To control them precisely, switch to Plotly or Altair.

Practical Tips

  • Cache the data, not the figure. Streamlit reruns your script on every widget change. Put loading and heavy computation behind @st.cache_data, and build the figure after it.
  • Close Matplotlib figures you create in loops. plt.close(fig) after st.pyplot(fig) avoids the memory growing as users interact.
  • Keep chart data long-format for Plotly and Altair, and wide for the built-in charts (one column per series).
  • Match the page width. Use width="stretch" for Plotly and Altair so the chart fills the column on any screen.
  • Interactive Plotly selections come back from st.plotly_chart(..., on_select="rerun"), which is how you build click-to-filter dashboards.

Similar Topics