AI如何创建Python可视化仪表板

如何创建python 可视化仪表板

创建Python可视化仪表板的方法

Python提供了多种创建可视化仪表板的工具和框架,以下是几种主要方法:

1. 使用Dash框架

Dash是由Plotly开发的Python框架,非常适合创建交互式仪表板。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 安装Dash
pip install dash

# 基本示例
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd

# 创建示例数据
df = pd.DataFrame({
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Amount": [4, 1, 2, 2, 4, 5],
"City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})

# 创建Dash应用
app = dash.Dash(__name__)

# 定义布局
app.layout = html.Div(children=[
html.H1(children='水果销售仪表板'),

dcc.Dropdown(
id='city-dropdown',
options=[{'label': city, 'value': city} for city in df['City'].unique()],
value='SF'
),

dcc.Graph(
id='example-graph'
)
])

# 定义回调函数
@app.callback(
dash.dependencies.Output('example-graph', 'figure'),
[dash.dependencies.Input('city-dropdown', 'value')]
)
def update_graph(selected_city):
filtered_df = df[df['City'] == selected_city]
fig = px.bar(filtered_df, x="Fruit", y="Amount", title=f"{selected_city}的水果销售")
return fig

if __name__ == '__main__':
app.run_server(debug=True)

2. 使用Streamlit

Streamlit是一个更简单的仪表板创建工具,适合快速原型开发。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 安装Streamlit
pip install streamlit

# 示例代码 (保存为app.py)
import streamlit as st
import pandas as pd
import plotly.express as px

# 标题
st.title('销售数据仪表板')

# 加载数据
df = pd.DataFrame({
"Product": ["A", "B", "C", "D", "E"],
"Sales": [100, 200, 150, 300, 250],
"Region": ["North", "South", "North", "East", "West"]
})

# 显示数据
st.write("原始数据:", df)

# 创建图表
fig = px.bar(df, x="Product", y="Sales", color="Region", title="产品销售情况")
st.plotly_chart(fig)

# 添加交互元素
selected_region = st.selectbox("选择地区", df['Region'].unique())
filtered_df = df[df['Region'] == selected_region]
st.write(f"{selected_region}地区销售数据:", filtered_df)

运行命令:streamlit run app.py

3. 使用Panel

Panel是另一个强大的仪表板库,支持多种绘图库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 安装Panel
pip install panel

# 示例代码
import panel as pn
import pandas as pd
import plotly.express as px

pn.extension('plotly')

# 创建数据
df = pd.DataFrame({
"x": [1, 2, 3, 4, 5],
"y": [2, 5, 3, 6, 4]
})

# 创建交互式小部件
slider = pn.widgets.IntSlider(name='点大小', start=1, end=10, value=5)

# 创建动态绘图函数
@pn.depends(slider.param.value)
def get_plot(size):
fig = px.scatter(df, x="x", y="y", size_max=size)
return fig

# 创建仪表板布局
dashboard = pn.Column(
"# 交互式仪表板",
slider,
get_plot
)

dashboard.servable()

运行命令:panel serve your_script.py --show

4. 使用Bokeh

Bokeh专注于现代Web浏览器的交互式可视化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 安装Bokeh
pip install bokeh

# 示例代码
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import ColumnDataSource, Select
from bokeh.layouts import column
from bokeh.io import curdoc

# 创建数据
data = {'x': [1, 2, 3, 4, 5], 'y': [6, 7, 2, 4, 5]}
source = ColumnDataSource(data=data)

# 创建绘图
plot = figure(title="简单线图", plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

# 创建下拉菜单
select = Select(title="选项:", options=["选项1", "选项2", "选项3"], value="选项1")

# 创建布局
layout = column(select, plot)

# 显示
curdoc().add_root(layout)

选择建议

  • 快速原型开发:Streamlit
  • 复杂交互式应用:Dash
  • 多绘图库支持:Panel
  • 专业可视化:Bokeh