- Contents
- st.line_chart
- element.add_rows
Display a line 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.line_chart does not guess the data specification correctly, try specifying your desired chart using st.altair_chart.
Function signature[source] | |
---|---|
st.line_chart(data=None, *, x=None, y=None, x_label=None, y_label=None, color=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 lines in this chart. For a line chart with just one line, this can be:
For a line chart with multiple lines, where the dataframe is in long format (that is, y is None or just one column), this can be:
For a line chart with multiple lines, where the dataframe is in wide format (that is, y is a Sequence of columns), this can be:
You can set the default colors in the theme.chartCategoryColors configuration option. |
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
Example 1: Basic line chart from a dataframe
If you don't use any of the optional parameters, Streamlit plots each column as a separate line, uses the index as the x values, and labels each series with the column name:
Example 2: Line chart from specific dataframe columns
You can choose different columns to use for the x and y values. If your dataframe is in long format (all y-values in one column), you can set the line colors from another column.
If the column contains color strings, the colors will be applied directly and the series will be unlabeled. If the column contains other values, those values will label each line, and the line colors will be selected from the default color palette. You can configure this color palette in the theme.chartCategoryColors configuration option.
Example 3: Line chart from wide-format dataframe
If your dataframe is in wide format (y-values are in multiple columns), you can pass a list of columns to the y parameter. Each column name becomes a series label. To override the default colors, pass a list of colors to the color parameter, one for each series:
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
You can do the same thing with plots. For example, if you want to add more data to a line chart:
And for plots whose datasets are named, you can pass the data with a keyword argument where the key is the name:
Still have questions?
Our forums are full of helpful information and Streamlit experts.