Display a bar 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 spec. As a result this is easier to use for many "just plot this" scenarios, while being less customizable.
If st.bar_chart does not guess the data specification correctly, try specifying your desired chart using st.altair_chart.
Function signature[source] | |
---|---|
st.bar_chart(data=None, *, x=None, y=None, color=None, width=0, height=0, use_container_width=True) | |
Parameters | |
data (pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, pyspark.sql.DataFrame, snowflake.snowpark.dataframe.DataFrame, snowflake.snowpark.table.Table, Iterable, or dict) | Data to be plotted. Pyarrow tables are not supported by Streamlit's legacy DataFrame serialization (i.e. with config.dataFrameSerialization = "legacy"). To use pyarrow tables, please enable pyarrow by changing the config setting, config.dataFrameSerialization = "arrow". |
x (str or None) | Column name to use for the x-axis. If None, uses the data index for the x-axis. This argument can only be supplied by keyword. |
y (str, sequence of str, or None) | Column name(s) to use for the y-axis. If a sequence of strings, draws several series on the same chart by melting your wide-format table into a long-format table behind the scenes. If None, draws the data of all remaining columns as data series. This argument can only be supplied by keyword. |
color (str, tuple, sequence of str, sequence of tuple, or None) | The color to use for different series in this chart. This argument can only be supplied by keyword. For a bar chart with just one series, this can be:
For a bar chart with multiple series, where the dataframe is in long format (that is, y is None or just one column), this can be:
For a bar chart with multiple series, where the dataframe is in wide format (that is, y is a sequence of columns), this can be:
|
width (int) | The chart width in pixels. If 0, selects the width automatically. This argument can only be supplied by keyword. |
height (int) | The chart height in pixels. If 0, selects the height automatically. This argument can only be supplied by keyword. |
use_container_width (bool) | If True, set the chart width to the column width. This takes precedence over the width argument. This argument can only be supplied by keyword. |
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.bar_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": list(range(20)) * 3, "col2": np.random.randn(60), "col3": ["A"] * 20 + ["B"] * 20 + ["C"] * 20, } ) st.bar_chart(chart_data, x="col1", y="col2", color="col3")Finally, 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( {"col1": list(range(20)), "col2": np.random.randn(20), "col3": np.random.randn(20)} ) st.bar_chart( chart_data, x="col1", y=["col2", "col3"], color=["#FF0000", "#0000FF"] # Optional )
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. Pyarrow tables are not supported by Streamlit's legacy DataFrame serialization (i.e. with config.dataFrameSerialization = "legacy"). To use pyarrow tables, please enable pyarrow by changing the config setting, config.dataFrameSerialization = "arrow". |
**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.