3 Chart Elements Explained
Key Concepts
- st.line_chart: Creates a line chart from data.
- st.bar_chart: Creates a bar chart from data.
- st.area_chart: Creates an area chart from data.
st.line_chart
st.line_chart
is used to create a line chart in Streamlit. This chart is ideal for visualizing trends over time or across categories. The data provided to this function should be in a format that can be easily plotted, such as a Pandas DataFrame.
Example:
import streamlit as st import pandas as pd data = pd.DataFrame({ 'year': [2010, 2011, 2012, 2013, 2014], 'sales': [100, 150, 200, 250, 300] }) st.line_chart(data.set_index('year'))
st.bar_chart
st.bar_chart
is used to create a bar chart in Streamlit. This chart is suitable for comparing categorical data. The data provided should be in a format that can be easily plotted, such as a Pandas DataFrame.
Example:
import streamlit as st import pandas as pd data = pd.DataFrame({ 'category': ['A', 'B', 'C', 'D'], 'value': [10, 24, 36, 40] }) st.bar_chart(data.set_index('category'))
st.area_chart
st.area_chart
is used to create an area chart in Streamlit. This chart is useful for visualizing cumulative data or trends over time. The data provided should be in a format that can be easily plotted, such as a Pandas DataFrame.
Example:
import streamlit as st import pandas as pd data = pd.DataFrame({ 'year': [2010, 2011, 2012, 2013, 2014], 'sales': [100, 150, 200, 250, 300] }) st.area_chart(data.set_index('year'))
Analogies
Think of st.line_chart
as a line graph in a stock market report, showing how a value changes over time. The st.bar_chart
is like a bar graph in a survey report, comparing different categories. The st.area_chart
is akin to a shaded area under a line graph, representing cumulative values over time.
By mastering these chart elements, you can create powerful visualizations in your Streamlit applications, making data more accessible and understandable to your users.