在Python中使用 Plotly 的 Sankey 图
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 plotly 是一个交互式可视化库。
桑基图
桑基图是一种流程图,其中箭头的宽度与流速相比较。桑基图还可以可视化表示源节点的源,目标节点的目标,设置流量的值和显示节点名称的标签。如果流量是两倍宽,则表示数量增加一倍。
示例 1:
Python3
fig = go.Figure(data=[go.Sankey(
node = dict(
thickness = 5,
line = dict(color = "green", width = 0.1),
label = ["A", "B", "C", "D", "E", "F"],
color = "blue"
),
link = dict(
# indices correspond to labels
source = [0, 6, 1, 4, 2, 3],
target = [2, 1, 5, 2, 1, 5],
value = [7, 1, 3, 6, 9, 4]
))])
fig.show()
Python3
fig = go.Figure(data=[go.Sankey(
node = dict(
thickness = 5,
line = dict(color = "green", width = 0.1),
label = ["A", "B", "C", "D", "E", "F"],
color = "blue"
),
link = dict(
# indices correspond to labels
source = [0, 6, 1, 4, 2, 3],
target = [2, 1, 5, 2, 1, 5],
value = [7, 1, 3, 6, 9, 4]
))])
fig.update_layout(
hovermode = 'x',
title="Sankey Diagram",
font=dict(size = 10, color = 'green'),
plot_bgcolor='blue',
paper_bgcolor='yellow'
)
fig.show()
输出:
示例 2:设置图表样式
Python3
fig = go.Figure(data=[go.Sankey(
node = dict(
thickness = 5,
line = dict(color = "green", width = 0.1),
label = ["A", "B", "C", "D", "E", "F"],
color = "blue"
),
link = dict(
# indices correspond to labels
source = [0, 6, 1, 4, 2, 3],
target = [2, 1, 5, 2, 1, 5],
value = [7, 1, 3, 6, 9, 4]
))])
fig.update_layout(
hovermode = 'x',
title="Sankey Diagram",
font=dict(size = 10, color = 'green'),
plot_bgcolor='blue',
paper_bgcolor='yellow'
)
fig.show()
输出: