环境:win7
1、若需要安装Anaconda则前往 https://www.anaconda.com/distribution/ 下载并按其文档说明进行安装
然后前往官网下载安装包Pyecharts
https://pypi.org/project/pyecharts/#files
2、将Pyecharts文件(也就是下载的pyecharts-1.6.2-py3-none-any.whl文件)放到Anaconda3的pkgs目录下
3、打开Anaconda Prompt,输入cd anaconda3/pkgs进入文件夹
4、输入pip install pyecharts-1.6.2-py3-none-any.whl即可安装成功
————————————————
版权声明:本文参考优快云博主「恪守不渝」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/weixin_44010678/article/details/88362093
本方案亲自验证有效。
本文以桑基图为例,进行绘制图形的实验。
什么是桑基图?请参考
https://baike.baidu.com/item/%E6%A1%91%E5%9F%BA%E5%9B%BE/10773057?fr=aladdin
桑基图有什么用?请参考
https://www.zhihu.com/question/45502919
接下来看如何绘制图形
按照旧代码添加
from pyecharts import Sankey
会报错:
ImportError: cannot import name 'Sankey' from 'pyecharts' (C:\ProgramData\Anaconda3\lib\site-packages\pyecharts\__init__.py)
然后改成下面桑基图代码例子进行import,则可以绘制任意定义好nodes节点、links边(包含起点、终点、边值的三元组)的桑基图了:
import asyncio
import pyecharts.options as opts
from pyecharts.charts import Sankey
async def get_json_data(url: str) -> dict:
async with ClientSession(connector=TCPConnector(ssl=False)) as session:
async with session.get(url=url) as response:
return await response.json()
# 获取官方的数据
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'},
],
links1 =[
{'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}
]#
(
Sankey(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add(
series_name="",
nodes=nodes1,
links=links1,
itemstyle_opts=opts.ItemStyleOpts(border_width=1, border_color="#aaa"),
linestyle_opt=opts.LineStyleOpts(color="source", curve=0.5, opacity=0.5),
tooltip_opts=opts.TooltipOpts(trigger_on="mousemove"),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Sankey Diagram"))
.render(path="F:/Python/sankey_diagram.html",delay=3)
)
更多绘图案例请参考github:
https://github.com/pyecharts/pyecharts-gallery