Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 Β· Text, Data and Charts

Built-in Charts

Draw charts in one line with st.line_chart, st.area_chart, st.bar_chart, st.scatter_chart and st.map. Learn wide versus long data, x and y arguments, colors and labels.

Streamlit's built-in chart functions are the fastest route from data to a picture: one call, one DataFrame, an interactive chart with tooltips, zoom and pan. They cover the everyday cases: lines, areas, bars, scatter plots and maps. When you need something more specialized you reach for Matplotlib, Altair or Plotly, which the next lesson covers.

The st. calls are real and read-only, with the resulting chart described in words. The runnable examples prepare data in pandas and draw the equivalent picture with Matplotlib, so you can see the shapes.

The five native charts

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

rng = np.random.default_rng(0)
df = pd.DataFrame(
    rng.normal(size=(30, 3)).cumsum(axis=0),
    columns=["north", "south", "east"],
)

st.line_chart(df)
st.area_chart(df)
st.bar_chart(df)

Given a DataFrame with three numeric columns and a 30-row index, line_chart draws three colored lines with the row number on the x axis and a legend of the column names. Hovering shows a tooltip with the values. area_chart fills under the lines, and bar_chart draws grouped (stacked by default) bars for each row. Charts can be zoomed with the mouse wheel and panned by dragging; a toolbar offers a fullscreen button.

Function Best for
st.line_chart Trends over time or an ordered x axis
st.area_chart Totals and composition over time (stacked areas)
st.bar_chart Comparing categories
st.scatter_chart Relationship between two numeric columns
st.map Points with latitude and longitude on a map

Wide data versus long data

The simplest input is wide data: the index is x, and each column is one series. Real data is often long (tidy): one row per observation, with a column naming the series.

date region sales
Mon north 10
Mon south 12
Tue north 14

For long data, tell the chart which columns to use:

Python
st.line_chart(long_df, x="date", y="sales", color="region")

This draws one line per region, colored automatically. You can pass y as a list of columns (y=["north", "south"]) for wide data and color as a column name for long data. When a color must be specific, pass a hex string or list: color=["#1f77b4", "#ff7f0e"].

Other useful arguments:

  • x_label and y_label: axis titles.
  • height: chart height in pixels.
  • horizontal=True (bar chart): bars run left to right, good for long category names.
  • stack=False (bar and area): grouped or overlapping instead of stacked.
  • width="stretch" (or use_container_width=True in older versions) to fill the column.

Reshape with pandas: wide to long and back

Converting between the shapes is a daily Streamlit task. Both directions use pandas (see reshaping):

Python Wide to long and back

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

wide goes straight into st.line_chart(wide). long goes in with x="day", y="sales", color="region". Use real dates (not text like "Mon") on the x axis for time series, so the chart knows the correct order.

Bar charts and categories

Python
import pandas as pd
import streamlit as st

sales = pd.DataFrame({
    "drink": ["Tea", "Coffee", "Juice", "Water"],
    "units": [120, 310, 80, 150],
})
st.bar_chart(sales, x="drink", y="units", horizontal=True, x_label="Units sold")

The page shows four horizontal bars, one per drink. Category order on a native chart is not something you fully control: depending on the version it may follow the data or the alphabet. Sorting by value (sales.sort_values("units", ascending=False)) is almost always clearer, so sort the data first, check the result, and if the order must be exact, specify it explicitly with Altair (next lesson).

Scatter charts

st.scatter_chart plots points; size and color map columns to dot size and color, which lets you show three or four variables at once:

Python
st.scatter_chart(cars, x="weight", y="mpg", color="origin", size="power")

The page shows a cloud of dots, colored by origin, each dot's area proportional to power. Hover shows the row's values. Use scatter charts to look for correlation, clusters and outliers.

Maps

st.map puts points on a map. The DataFrame needs columns named lat and lon (or latitude and longitude):

Python
import pandas as pd
import streamlit as st

cities = pd.DataFrame({
    "lat": [59.91, -12.05, 18.52],
    "lon": [10.75, -77.04, 73.86],
})
st.map(cities)

The page shows a map with three dots, over Oslo, Lima and Pune, and it zooms to fit them. Optional size and color columns style the dots. For lines, arcs, 3D columns or hexagon bins use st.pydeck_chart. Maps load tiles from the internet, so they need network access when viewed.

Draw the same pictures (runnable Matplotlib)

Streamlit's native charts use the Vega-Lite engine in the browser. In this lesson's runnable block, Matplotlib draws equivalent pictures from the same DataFrame so you can see a line chart and a horizontal bar chart. This is a stand-in for the look, not what Streamlit calls internally.

Python Line and bar charts from a DataFrame (Matplotlib)

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing the right chart

Match the chart to the question

Over time? Line or area chart
Compare groups? Bar chart
Two numbers? Scatter chart
Where? Map
1 / 2

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. Your data has columns date, region and sales (one row per region and day). How do you draw one line per region?

    For long data, set x and y and use color to split the series.
  2. 2. Which column names does st.map look for?

    st.map expects lat/lon (or latitude/longitude) columns.
  3. 3. Which function reshapes long data to wide data in pandas?

    pivot turns a column's values into separate columns; melt does the opposite.
  4. 4. What does st.scatter_chart's size argument do?

    size takes a column name (or a number) controlling dot size.

Summary

  • st.line_chart, area_chart, bar_chart, scatter_chart and map draw interactive charts from one DataFrame.
  • Wide data uses the index as x and one column per series; long data uses x, y and color.
  • Sort the data before charting to control bar order; label axes with x_label and y_label.
  • Use Matplotlib, Altair or Plotly when the built-ins are not enough.

Next: Matplotlib, Altair and Plotly in Streamlit