基于python的桑基图的绘制
2018-09-11 本文已影响42人
画长空_yin
桑基图用来表示某个事物经过多个过程而变为最终的某个东西,中间过程是怎么转换的,每个过程的比例大致是怎么样了,体现了由源到头的转换关系,本文借用python中的pyecharts库来进行绘制。
具体库的安装请自行百度,以下是代码:
from __future__ import unicode_literals
from pyecharts import Sankey
nodes1 = [
{'name': 'Songhua_PPT'}, {'name': 'Songhua_RWCN'}, {'name': 'Songhua_CWC'},
{'name': 'Songhua_IWCC'}, {'name': 'Songhua_RWCC'},{'name': 'Songhua_ET'},
{'name': 'Songhua_FWS'},
{'name': 'Liao_PPT'}, {'name': 'Liao_RWCN'}, {'name': 'Liao_CWC'},
{'name': 'Liao_IWCC'}, {'name': 'Liao_RWCC'},{'name': 'Liao_ET'},
{'name': 'Liao_FWS'},
{'name': 'Hai_PPT'}, {'name': 'Hai_RWCN'}, {'name': 'Hai_CWC'},
{'name': 'Hai_IWCC'}, {'name': 'Hai_RWCC'},{'name': 'Hai_ET'},
{'name': 'Hai_FWS'},
{'name': 'Yellow_PPT'}, {'name': 'Yellow_RWCN'}, {'name': 'Yellow_CWC'},
{'name': 'Yellow_IWCC'}, {'name': 'Yellow_RWCC'},{'name': 'Yellow_ET'},
{'name': 'Yellow_FWS'},
{'name': 'Huai_PPT'}, {'name': 'Huai_RWCN'}, {'name': 'Huai_CWC'},
{'name': 'Huai_IWCC'}, {'name': 'Huai_RWCC'},{'name': 'Huai_ET'},
{'name': 'Huai_FWS'},
{'name': 'Yangtze_PPT'}, {'name': 'Yangtze_RWCN'}, {'name': 'Yangtze_CWC'},
{'name': 'Yangtze_IWCC'}, {'name': 'Yangtze_RWCC'},{'name': 'Yangtze_ET'},
{'name': 'Yangtze_FWS'},
{'name': 'Pearl_PPT'}, {'name': 'Pearl_RWCN'}, {'name': 'Pearl_CWC'},
{'name': 'Pearl_IWCC'}, {'name': 'Pearl_RWCC'},{'name': 'Pearl_ET'},
{'name': 'Pearl_FWS'},
]
links = [
{'source': 'Songhua_ET', 'target': 'Songhua_RWCN', 'value': 371.75},
{'source': 'Songhua_ET', 'target': 'Songhua_FWS', 'value': 104.92},
{'source': 'Songhua_PPT', 'target': 'Songhua_ET', 'value':448.01},
{'source': 'Songhua_ET', 'target': 'Songhua_CWC', 'value': 76.25},
{'source': 'Songhua_CWC', 'target': 'Songhua_IWCC', 'value': 10.37},
{'source': 'Songhua_CWC', 'target': 'Songhua_RWCC', 'value': 65.88},
{'source': 'Liao_ET', 'target': 'Liao_RWCN', 'value': 389.05},
{'source': 'Liao_ET', 'target': 'Liao_FWS', 'value': 84.44},
{'source': 'Liao_PPT', 'target': 'Liao_ET', 'value':496.07},
{'source': 'Liao_ET', 'target': 'Liao_CWC', 'value': 107.021},
{'source': 'Liao_CWC', 'target': 'Liao_IWCC', 'value': 18.69},
{'source': 'Liao_CWC', 'target': 'Liao_RWCC', 'value': 88.32},
{'source': 'Hai_ET', 'target': 'Hai_RWCN', 'value': 285.70},
{'source': 'Hai_ET', 'target': 'Hai_FWS', 'value': 120.27},
{'source': 'Hai_PPT', 'target': 'Hai_ET', 'value':450.83},
{'source': 'Hai_ET', 'target': 'Hai_CWC', 'value': 165.12},
{'source': 'Hai_CWC', 'target': 'Hai_IWCC', 'value': 43.38},
{'source': 'Hai_CWC', 'target': 'Hai_RWCC', 'value': 121.74},
{'source': 'Yellow_ET', 'target': 'Yellow_RWCN', 'value': 347.577},
{'source': 'Yellow_ET', 'target': 'Yellow_FWS', 'value': 44.624},
{'source': 'Yellow_PPT', 'target': 'Yellow_ET', 'value':412.6816},
{'source': 'Yellow_ET', 'target': 'Yellow_CWC', 'value': 65.1038},
{'source': 'Yellow_CWC', 'target': 'Yellow_IWCC', 'value': 13.5165},
{'source': 'Yellow_CWC', 'target': 'Yellow_RWCC', 'value': 51.5873},
{'source': 'Huai_ET', 'target': 'Huai_RWCN', 'value': 365.67},
{'source': 'Huai_ET', 'target': 'Huai_FWS', 'value': 250.32},
{'source': 'Huai_PPT', 'target': 'Huai_ET', 'value':711.66},
{'source': 'Huai_ET', 'target': 'Huai_CWC', 'value': 345.99},
{'source': 'Huai_CWC', 'target': 'Huai_IWCC', 'value': 90.74},
{'source': 'Huai_CWC', 'target': 'Huai_RWCC', 'value': 255.24},
{'source': 'Yangtze_ET', 'target': 'Yangtze_RWCN', 'value': 696.42},
{'source': 'Yangtze_ET', 'target': 'Yangtze_FWS', 'value': 278.12},
{'source': 'Yangtze_PPT', 'target': 'Yangtze_ET', 'value':831.75},
{'source': 'Yangtze_ET', 'target': 'Yangtze_CWC', 'value': 135.32},
{'source': 'Yangtze_CWC', 'target': 'Yangtze_IWCC', 'value': 33.17},
{'source': 'Yangtze_CWC', 'target': 'Yangtze_RWCC', 'value': 102.15},
{'source': 'Pearl_ET', 'target': 'Pearl_RWCN', 'value': 1151.49},
{'source': 'Pearl_ET', 'target': 'Pearl_FWS', 'value': 210.62},
{'source': 'Pearl_PPT', 'target': 'Pearl_ET', 'value':1376.94},
{'source': 'Pearl_ET', 'target': 'Pearl_CWC', 'value': 225.44},
{'source': 'Pearl_CWC', 'target': 'Pearl_IWCC', 'value': 45.88},
{'source': 'Pearl_CWC', 'target': 'Pearl_RWCC', 'value': 179.60}
] #
snakey=Sankey("",title_pos='center',width=1200,height=1000)
snakey.add(
"",
nodes1,
links,
line_opacity=1,
line_curve=0.8,
line_color="source",
is_label_show=True,
sankey_node_width=30,
sankey_node_gap=8,
label_pos="right",
)
snakey.render(path="D:/python example/my_七个流域_chart.pdf",delay=3)
结果如下:
image.png