使用 pyecharts 渲染成图片一直是开发者比较关心的功能,pyecharts提供了 selenium、phantomjs 和 pyppeteer 三种方式。
更多介绍可以学习官方文档:https://pyecharts.org/#/zh-cn/render_images
首先需要安装上snapshot-selenium
代码语言:javascript复制pip install snapshot-selenium -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
测试代码如下:
代码语言:javascript复制from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from pyecharts import options as opts
from pyecharts.charts import Sankey
sankey = Sankey(
init_opts=opts.InitOpts(
width='1000px',
height='600px',
bg_color='#fff'
)
)
sankey.add(
'',
nodes,
links,
node_gap=0,
node_width=80,
pos_right='5%',
node_align='justify',
focus_node_adjacency=True,
linestyle_opt=opts.LineStyleOpts(curve=0.5, opacity=0.2, color="source"),
label_opts=opts.LabelOpts(position='inside', color='white'),
itemstyle_opts=opts.ItemStyleOpts(border_color="#fff"),
)
print(":".join(["CSDN叶庭云", "https://yetingyun.blog.csdn.net/"]))
# sankey.render("./results/009.html")
make_snapshot(snapshot, sankey.render(), "Pyecharts生成图片.png")
关键代码:
代码语言:javascript复制from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
# 渲染的html保存为png图片
make_snapshot(snapshot, sankey.render(), "Pyecharts生成图片.png")
结果如下: