如何在Power BI或其他程序中创建只显示员工地点转移的桑基图?
1 个回答
1
我觉得桑基图可能不太适合这样设计。你可能会期待有多个相同的节点,这样会让图看起来很混乱,甚至可能看不懂(如果我理解你的意思的话)。
也许圆形流动图会更好?就是把桑基图做成一个圆圈的样子。
有一个Python库可以做到这一点,如下所示:
pip install pycirclize
import pandas as pd
from pycirclize import Circos
cities = [
"London",
"Milan",
"Tokyo",
"Brussels",
"Berlin",
"Riyadh",
"Shanghai",
"Hong Kong",
"Seoul",
"Dubai",
"New York",
"Sydney",
"Singapore",
"Brisbane",
"Melbourne",
"Kuala Lumpur",
"Perth",
]
# create route matrix
row_names = cities
col_names = cities
matrix_data = [
[0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
]
matrix_df = pd.DataFrame(matrix_data, index=row_names, columns=col_names)
# Initialize Circos from matrix for plotting Chord Diagram
circos = Circos.initialize_from_matrix(
matrix_df,
space=5,
cmap="tab10",
label_kws=dict(size=12),
link_kws=dict(ec="black", lw=0.5, direction=1),
)
circos.savefig("circlize_plot.png")
你可以把row_names
和col_names
简化成只包含From
和To
列中的城市,但我只是做了一个粗略的版本。你也可以让这个矩阵根据输入自动生成。