天天看點

實用技巧 | Pyecharts可視化渲染為圖檔儲存

使用 pyecharts 渲染成圖檔一直是開發者比較關心的功能,pyecharts提供了 selenium、phantomjs 和 pyppeteer 三種方式。

更多介紹可以學習官方文檔:https://pyecharts.org/#/zh-cn/render_images

首先需要安裝上snapshot-selenium

pip install snapshot-selenium -i http://pypi.douban.com/simple --trusted-host pypi.douban.com           

複制

實用技巧 | Pyecharts可視化渲染為圖檔儲存

測試代碼如下:

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")           

複制

關鍵代碼:

from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot

# 渲染的html儲存為png圖檔
make_snapshot(snapshot, sankey.render(), "Pyecharts生成圖檔.png")           

複制

結果如下:

實用技巧 | Pyecharts可視化渲染為圖檔儲存