桑基图DEMO代码
代码取自pyecharts.charts下sankey的example代码。
nodes = [
{'name': 'category1'},
{'name': 'category2'},
{'name': 'category3'},
{'name': 'category4'},
{'name': 'category5'},
{'name': 'category6'},
]
#nodes需要把桑基图中出现的名称全部设置进去,并且要保证links中的名称与name相同
links = [
{'source': 'category1', 'target': 'category2', 'value': 10},
{'source': 'category2', 'target': 'category3', 'value': 15},
{'source': 'category3', 'target': 'category4', 'value': 20},
{'source': 'category5', 'target': 'category6', 'value': 25}
]
#links代表节点关系,source表示起点,target表示终点,需要将节点关系全部输入进去,value表示节点长度
from pyecharts import options as opts
from pyecharts.charts import Page, Sankey
sankey = Sankey() #可以设置大小和图标名称
sankey.add(
'sankey', #名称
nodes, #输入节点,如果导入json数据,nodes=json['nodes]
links, #输入关系,nodes=json['links']
linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source", width=1600),
label_opts=opts.LabelOpts(position="right", is_show=True, color='red'),
node_gap=20
)
sankey.render()
运行的结果如下: