streamlit数据和绘图
目录st.dataframe
数据展示
支持(pandas.DataFrame, pandas.Series, pandas.Styler, pandas.Index, pyarrow.Table, numpy.ndarray, pyspark.sql.DataFrame, snowflake.snowpark.dataframe.DataFrame, snowflake.snowpark.table.Table, Iterable, dict, or None) 等
import streamlit as st
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(50, 20), columns=("col %d" % i for i in range(20)))
st.dataframe(df) # Same as st.write(df)
st.data_editor
数据编辑
import streamlit as st
import pandas as pd
df = pd.DataFrame(
[
{"command": "st.selectbox", "rating": 4, "is_widget": True},
{"command": "st.balloons", "rating": 5, "is_widget": False},
{"command": "st.time_input", "rating": 3, "is_widget": True},
]
)
edited_df = st.data_editor(df)
favorite_command = edited_df.loc[edited_df["rating"].idxmax()]["command"] # 对核心数据进行标记
st.column_config
官方:https://docs.streamlit.io/library/api-reference/data/st.column_config
st.column_config.Column 配置通用数据列
st.column_config.TextColumn 配置文本数据列
st.column_config.NumberColumn 配置数值数据列
st.column_config.CheckboxColumn 配置复选框
st.column_config.SelectboxColumn 配置下拉单选框
st.column_config.DatetimeColumn 配置时间日期
st.column_config.DateColumn 配置日期
st.column_config.TimeColumn 配置时间
st.column_config.ListColumn 配置列表
st.column_config.LinkColumn 配置链接
st.column_config.ImageColumn 配置图像
st.column_config.LineChartColumn 配置线性图表
st.column_config.BarChartColumn 配置柱状图
st.column_config.ProgressColumn 配置进度条
# 实例 在表格中添加线性表格
import pandas as pd
import streamlit as st
data_df = pd.DataFrame(
{
"sales": [
[0, 4, 26, 80, 100, 40],
[80, 20, 80, 35, 40, 100],
[10, 20, 80, 80, 70, 0],
[10, 100, 20, 100, 30, 100],
],
}
)
st.data_editor(
data_df,
column_config={
"sales": st.column_config.LineChartColumn(
"Sales (last 6 months)",
width="medium",
help="The sales volume in the last 6 months",
y_min=0,
y_max=100,
),
},
hide_index=True,
)
st.metric
st.metric(label, value, delta=None, delta_color="normal", help=None, label_visibility="visible")
显示指标,并带有指示指标如何变化的可选指示符。
import streamlit as st
col1, col2, col3 = st.columns(3)
col1.metric("Temperature", "70 °F", "1.2 °F")
col2.metric("Wind", "9 mph", "-8%")
col3.metric("Humidity", "86%", "4%")
st.line_chart
直接绘制线状图
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.line_chart(chart_data)
动态绘制线状图
import time
import streamlit as st
import numpy as np
last_rows = np.random.randn(2, 2) # 初始化 一个点
chart = st.line_chart(last_rows)
print(last_rows)
for i in range(1, 101):
new_rows = last_rows[-1, :] + np.random.randn(5, 2).cumsum(axis=0) # 在上一点
chart.add_rows(new_rows)
last_rows = new_rows
time.sleep(0.1)
st.bar_chart
绘制柱状图
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)
st.area_chart
绘制面积图
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)
st.scatter_chart
散点图
import streamlit as st
import pandas as pd
import numpy as np
chart_data = pd.DataFrame(np.random.randn(80, 3 ), columns=["x","data","size"])
chart_data['color'] = np.random.choice(['A','B','C'], 80) # 随机选择
st.scatter_chart(
chart_data,
x='x',
y='data',
color='color',
size='size',
)
st.pyplot
依托于matplotlib自定义绘图
st.plotly_chart
依托 Plotly 自定义绘图,Plotly 是一个强大而受欢迎的 Python 库,用于创建交互式图表和可视化。
st.plotly_chart 函数还支持其他参数,可以进一步定制图表的外观和行为
st.plotly_chart(fig, theme="streamlit", use_container_width=True)
theme:设置图表的主题。可以设置为 “streamlit”(使用 Streamlit 的默认主题)或 None(使用 Plotly 的原生主题)。
sharing:设置图表的共享模式。可以设置为 “streamlit”(共享数据和回调函数)或 “auto”(自动检测共享模式)。
config:设置 Plotly 的配置选项。可以通过字典传递多个配置选项。
import streamlit as st
import plotly.express as px
# 添加直方图数据
x1 = np.random.randn(200) - 2
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 2
# 将数据分组在一起
hist_data = [x1, x2, x3]
group_labels = ['Group 1', 'Group 2', 'Group 3']
# 使用自定义 bin_size 创建 distplot
fig = ff.create_distplot(hist_data, group_labels, bin_size=[.1, .25, .5])
# 绘制图表
st.plotly_chart(fig, use_container_width=True)
streamlit_echarts插件
import streamlit as st
from streamlit_echarts import st_echarts
st.set_page_config(layout="wide")
option = {
"legend": {},
"tooltip": {
"trigger": 'axis',
"showContent": "false"
},
"dataset": {
"source": [
['销量', '2015', '2016', '2017', '2018', '2019', '2020'],
['春天', 56.5, 82.1, 88.7, 70.1, 53.4, 85.1],
['夏天', 51.1, 51.4, 55.1, 53.3, 73.8, 68.7],
['秋天', 40.1, 62.2, 69.5, 36.4, 45.2, 32.5],
['冬天', 25.2, 37.1, 41.2, 18, 33.9, 49.1]
]
},
"xAxis": {"type": 'category'},
"yAxis": {"gridIndex": 0},
"grid": {"top": '55%'},
"series": [
{"type": 'line', "smooth": "true", "seriesLayoutBy": 'row', "emphasis": {"focus": 'series'}},
{"type": 'line', "smooth": "true", "seriesLayoutBy": 'row', "emphasis": {"focus": 'series'}},
{"type": 'line', "smooth": "true", "seriesLayoutBy": 'row', "emphasis": {"focus": 'series'}},
{"type": 'line', "smooth": "true", "seriesLayoutBy": 'row', "emphasis": {"focus": 'series'}},
{
"type": 'pie',
"id": 'pie',
"radius": "25%",
"center": ['50%', '30%'],
"emphasis": {"focus": 'data'},
"label": {
"formatter": '{b}: {@2015} ({d}%)'
},
"encode": {
"itemName": '销量',
"value": '2015',
"tooltip": '2015'
}
}
],
"tooltip": {
"show": "true",
},
"label": {
"show":"true"
},
};
st_echarts(options=option)
参考资料
https://docs.streamlit.io/library/get-started
https://zhuanlan.zhihu.com/p/390136189
标签:data,chart,st,绘图,import,streamlit,config,数据 From: https://www.cnblogs.com/tian777/p/17802703.html