Pyecharts-桑基图

桑基图

导入需要的包

1
2
3
4
import pandas as pd
from pyecharts.charts import Sankey
from pyecharts import options as opts
import os

导入数据

1
df: pd.DataFrame = pd.read_excel(r'山西大学2021年本科生转专业情况.xlsx')

节点处理

1
2
3
4
5
nodes = list(set(df['转出专业'].tolist() + df['转入专业'].tolist() + df['类型'].tolist()))
node_list = []
for i in range(len(nodes)):
dict = {'name': nodes[i]}
node_list.append(dict)

连线数据处理

第一层

1
2
3
4
5
6
df1: pd.DataFrame = df[['转出专业', '转入专业']]
df1['次数'] = ''
for i in range(len(df1)):
df1.loc[i, '次数'] = df1[(df1['转出专业'] == df1.loc[i, '转出专业']) & (df1['转入专业'] == df1.loc[i, '转入专业'])].shape[0]
df1 = df1.drop_duplicates()
df1.columns = ['source', 'target', 'value']

第二层

1
2
3
4
5
6
df2: pd.DataFrame = df[['转入专业', '类型']]
df2['次数'] = ''
for i in range(len(df2)):
df2.loc[i, '次数'] = df2[(df2['转入专业'] == df2.loc[i, '转入专业']) & (df2['类型'] == df2.loc[i, '类型'])].shape[0]
df2 = df2.drop_duplicates()
df2.columns = ['source', 'target', 'value']

拼接两个数据

1
2
3
4
5
6
7
8
link_df = pd.concat([df1, df2])

link_list = []
for i in range(len(link_df)):
dict = {'source': link_df.iloc[i, 0],
'target': link_df.iloc[i, 1],
'value': int(link_df.iloc[i, 2])}
link_list.append(dict)

作图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sankey = (Sankey(init_opts=opts.InitOpts(bg_color='white'))
.add("",
nodes=node_list,
links=link_list,
pos_left="7%",
node_width=135,
node_gap=0,
layout_iterations=1000,
is_draggable=False,
focus_node_adjacency=True,
linestyle_opt=opts.LineStyleOpts(opacity=0.7, curve=0.5, color="source"),
label_opts=opts.LabelOpts(font_size=12, position='inside')
)
.set_global_opts(title_opts=opts.TitleOpts(title="转专业-桑基图"))
)

sankey.render("sankey.html")
os.system("sankey.html")

修改颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
node_list = [
{'name': '建筑电气与智能化', 'itemStyle': {'color': '#FFFFD9'}},
{'name': '能源与动力工程', 'itemStyle': {'color': '#F4FBC2'}},
{'name': '环境科学与工程类', 'itemStyle': {'color': '#D1EDB3'}},
{'name': '建筑环境与能源应用工程', 'itemStyle': {'color': '#E7F5B1'}},
{'name': '机械电子工程', 'itemStyle': {'color': '#D1EDB3'}},
{'name': '生物科学类', 'itemStyle': {'color': '#89D1BA'}},
{'name': '工商管理类', 'itemStyle': {'color': '#41B6C4'}},
{'name': '化学类', 'itemStyle': {'color': '#64C3BE'}},
{'name': '电气工程及其自动化', 'itemStyle': {'color': '#2CA0C1'}, 'label': {'color': 'white'}},
{'name': '计算机科学与技术', 'itemStyle': {'color': '#206CAE'}, 'label': {'color': 'white'}},
{'name': '电子信息工程', 'itemStyle': {'color': '#1D89BC'}, 'label': {'color': 'white'}},
{'name': '数据科学与大数据技术', 'itemStyle': {'color': '#2252A2'}, 'label': {'color': 'white'}},
{'name': '经济学类', 'itemStyle': {'color': '#243A96'}, 'label': {'color': 'white'}},
{'name': '成绩优秀', 'itemStyle': {'color': '#182A7A'}, 'label': {'color': 'white'}},
{'name': '其他情况', 'itemStyle': {'color': '#081D58'}, 'label': {'color': 'white'}},
]

完整代码

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pandas as pd
from pyecharts.charts import Sankey
from pyecharts import options as opts
import os

df: pd.DataFrame = pd.read_excel(r'山西大学2021年本科生转专业情况.xlsx')

# -------------------------------处理节点--------------------------------------
nodes = list(set(df['转出专业'].tolist() + df['转入专业'].tolist() + df['类型'].tolist()))
node_list = []
for i in range(len(nodes)):
dict = {'name': nodes[i]}
node_list.append(dict)
# print(node_list)

node_list = [
{'name': '建筑电气与智能化', 'itemStyle': {'color': '#FFFFD9'}},
{'name': '能源与动力工程', 'itemStyle': {'color': '#F4FBC2'}},
{'name': '环境科学与工程类', 'itemStyle': {'color': '#D1EDB3'}},
{'name': '建筑环境与能源应用工程', 'itemStyle': {'color': '#E7F5B1'}},
{'name': '机械电子工程', 'itemStyle': {'color': '#D1EDB3'}},
{'name': '生物科学类', 'itemStyle': {'color': '#89D1BA'}},
{'name': '工商管理类', 'itemStyle': {'color': '#41B6C4'}},
{'name': '化学类', 'itemStyle': {'color': '#64C3BE'}},
{'name': '电气工程及其自动化', 'itemStyle': {'color': '#2CA0C1'}, 'label': {'color': 'white'}},
{'name': '计算机科学与技术', 'itemStyle': {'color': '#206CAE'}, 'label': {'color': 'white'}},
{'name': '电子信息工程', 'itemStyle': {'color': '#1D89BC'}, 'label': {'color': 'white'}},
{'name': '数据科学与大数据技术', 'itemStyle': {'color': '#2252A2'}, 'label': {'color': 'white'}},
{'name': '经济学类', 'itemStyle': {'color': '#243A96'}, 'label': {'color': 'white'}},
{'name': '成绩优秀', 'itemStyle': {'color': '#182A7A'}, 'label': {'color': 'white'}},
{'name': '其他情况', 'itemStyle': {'color': '#081D58'}, 'label': {'color': 'white'}},
]

# -------------------------------处理连线--------------------------------------
df1: pd.DataFrame = df[['转出专业', '转入专业']]
df1['次数'] = ''
for i in range(len(df1)):
df1.loc[i, '次数'] = df1[(df1['转出专业'] == df1.loc[i, '转出专业']) & (df1['转入专业'] == df1.loc[i, '转入专业'])].shape[0]
df1 = df1.drop_duplicates()
df1.columns = ['source', 'target', 'value']
# print(df1)

df2: pd.DataFrame = df[['转入专业', '类型']]
df2['次数'] = ''
for i in range(len(df2)):
df2.loc[i, '次数'] = df2[(df2['转入专业'] == df2.loc[i, '转入专业']) & (df2['类型'] == df2.loc[i, '类型'])].shape[0]
df2 = df2.drop_duplicates()
df2.columns = ['source', 'target', 'value']

link_df = pd.concat([df1, df2])

link_list = []
for i in range(len(link_df)):
dict = {'source': link_df.iloc[i, 0],
'target': link_df.iloc[i, 1],
'value': int(link_df.iloc[i, 2])}
link_list.append(dict)
# print(link_list)

# -------------------------------画图--------------------------------------
sankey = (Sankey(init_opts=opts.InitOpts(bg_color='white'))
.add("",
nodes=node_list,
links=link_list,
pos_left="7%",
node_width=135,
node_gap=0,
layout_iterations=1000,
is_draggable=False,
focus_node_adjacency=True,
linestyle_opt=opts.LineStyleOpts(opacity=0.7, curve=0.5, color="source"),
label_opts=opts.LabelOpts(font_size=12, position='inside')
)
.set_global_opts(title_opts=opts.TitleOpts(title="转专业-桑基图"))
)

sankey.render("sankey.html")
os.system("sankey.html")

大功告成!

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2022-2023 发现美的眼睛
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信