Display an area chart.
This is syntax-sugar around st.altair_chart. The main difference is this command uses the data's own column and indices to figure out the chart's Altair spec. As a result this is easier to use for many "just plot this" scenarios, while being less customizable.
If st.area_chart does not guess the data specification correctly, try specifying your desired chart using st.altair_chart.
Function signature[source] | |
---|---|
st.area_chart(data=None, *, x=None, y=None, x_label=None, y_label=None, color=None, stack=None, width=None, height=None, use_container_width=True) | |
Parameters | |
data (Anything supported by st.dataframe) | Data to be plotted. |
x (str or None) | Column name or key associated to the x-axis data. If x is None (default), Streamlit uses the data index for the x-axis values. |
y (str, Sequence of str, or None) | Column name(s) or key(s) associated to the y-axis data. If this is None (default), Streamlit draws the data of all remaining columns as data series. If this is a Sequence of strings, Streamlit draws several series on the same chart by melting your wide-format table into a long-format table behind the scenes. |
x_label (str or None) | The label for the x-axis. If this is None (default), Streamlit will use the column name specified in x if available, or else no label will be displayed. |
y_label (str or None) | The label for the y-axis. If this is None (default), Streamlit will use the column name(s) specified in y if available, or else no label will be displayed. |
color (str, tuple, Sequence of str, Sequence of tuple, or None) | The color to use for different series in this chart. For an area chart with just 1 series, this can be:
For an area chart with multiple series, where the dataframe is in long format (that is, y is None or just one column), this can be:
For an area chart with multiple series, where the dataframe is in wide format (that is, y is a Sequence of columns), this can be:
|
stack (bool, "normalize", "center", or None) | Whether to stack the areas. If this is None (default), Streamlit uses Vega's default. Other values can be as follows:
|
width (int or None) | Desired width of the chart expressed in pixels. If width is None (default), Streamlit sets the width of the chart to fit its contents according to the plotting library, up to the width of the parent container. If width is greater than the width of the parent container, Streamlit sets the chart width to match the width of the parent container. To use width, you must set use_container_width=False. |
height (int or None) | Desired height of the chart expressed in pixels. If height is None (default), Streamlit sets the height of the chart to fit its contents according to the plotting library. |
use_container_width (bool) | Whether to override width with the width of the parent container. If use_container_width is True (default), Streamlit sets the width of the chart to match the width of the parent container. If use_container_width is False, Streamlit sets the chart's width according to width. |
Examples
import streamlit as st import pandas as pd import numpy as np chart_data = pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"]) st.area_chart(chart_data)You can also choose different columns to use for x and y, as well as set the color dynamically based on a 3rd column (assuming your dataframe is in long format):
import streamlit as st import pandas as pd import numpy as np chart_data = pd.DataFrame( { "col1": np.random.randn(20), "col2": np.random.randn(20), "col3": np.random.choice(["A", "B", "C"], 20), } ) st.area_chart(chart_data, x="col1", y="col2", color="col3")If your dataframe is in wide format, you can group multiple columns under the y argument to show multiple series with different colors:
import streamlit as st import pandas as pd import numpy as np chart_data = pd.DataFrame( np.random.randn(20, 3), columns=["col1", "col2", "col3"] ) st.area_chart( chart_data, x="col1", y=["col2", "col3"], color=["#FF0000", "#0000FF"], # Optional )You can adjust the stacking behavior by setting stack. Create a steamgraph:
import streamlit as st from vega_datasets import data source = data.unemployment_across_industries() st.area_chart(source, x="date", y="count", color="series", stack="center")
Function signature[source] | |
---|---|
element.add_rows(data=None, **kwargs) | |
Parameters | |
data (pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, pyspark.sql.DataFrame, snowflake.snowpark.dataframe.DataFrame, Iterable, dict, or None) | Table to concat. Optional. |
**kwargs (pandas.DataFrame, numpy.ndarray, Iterable, dict, or None) | The named dataset to concat. Optional. You can only pass in 1 dataset (including the one in the data parameter). |
Example
import streamlit as st import pandas as pd import numpy as np df1 = pd.DataFrame( np.random.randn(50, 20), columns=("col %d" % i for i in range(20)) ) my_table = st.table(df1) df2 = pd.DataFrame( np.random.randn(50, 20), columns=("col %d" % i for i in range(20)) ) my_table.add_rows(df2) # Now the table shown in the Streamlit app contains the data for # df1 followed by the data for df2.You can do the same thing with plots. For example, if you want to add more data to a line chart:
# Assuming df1 and df2 from the example above still exist... my_chart = st.line_chart(df1) my_chart.add_rows(df2) # Now the chart shown in the Streamlit app contains the data for # df1 followed by the data for df2.And for plots whose datasets are named, you can pass the data with a keyword argument where the key is the name:
my_chart = st.vega_lite_chart( { "mark": "line", "encoding": {"x": "a", "y": "b"}, "datasets": { "some_fancy_name": df1, # <-- named dataset }, "data": {"name": "some_fancy_name"}, } ) my_chart.add_rows(some_fancy_name=df2) # <-- name used as keyword
Still have questions?
Our forums are full of helpful information and Streamlit experts.