Pyecharts可視化圖片渲染的方法詳解
使用 pyecharts 渲染成圖片一直是開發(fā)者比較關心的功能,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

測試代碼如下:
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不僅能進行可視化圖片渲染,還能進行圖表的渲染,同樣也是使用selenium, phantomjs 和 pyppeteer這三種方式
渲染圖片依賴庫
1.make_snapshot
make_snapshot 用于 pyecharts 直接生成圖片。
from pyecharts.render import make_snapshot
def make_snapshot(
# 渲染引擎,可選 selenium 或者 phantomjs
engine: Any,
# 傳入 HTML 文件路徑
file_name: str,
# 輸出圖片路徑
output_name: str,
# 延遲時間,避免圖還沒渲染完成就生成了圖片,造成圖片不完整
delay: float = 2,
# 像素比例,用于調節(jié)圖片質量
pixel_ratio: int = 2,
# 渲染完圖片是否刪除原 HTML 文件
is_remove_html: bool = False,
# 瀏覽器類型,目前僅支持 Chrome, Safari,使用 snapshot-selenium 時有效
browser: str = "Chrome",
**kwargs,
)渲染方式
1.snapshot-selenium
snapshot-selenium 是 pyecharts + selenium 渲染圖片的擴展,使用 selenium 需要配置 browser driver,這部分可以參考 selenium-python 相關介紹,推薦使用 Chrome 瀏覽器,可以開啟 headless 模式。目前支持 Chrome, Safari。
# 安裝
pip install snapshot-selenium
# 使用方式
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
def bar_chart() -> Bar:
c = (
Bar()
.add_xaxis(["襯衫", "毛衣", "領帶", "褲子", "風衣", "高跟鞋", "襪子"])
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-測試渲染圖片"))
)
return c
make_snapshot(snapshot, bar_chart().render(), "bar0.png")2.snapshot-phantomjs
snapshot-phantomjs 是 pyecharts + phantomjs 渲染圖片的擴展,需要先安裝 phantomjs。
# 安裝
pip install snapshot-phantomjs
# 使用方式
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
def bar_chart() -> Bar:
c = (
Bar()
.add_xaxis(["襯衫", "毛衣", "領帶", "褲子", "風衣", "高跟鞋", "襪子"])
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-測試渲染圖片"))
)
return c
make_snapshot(snapshot, bar_chart().render(), "bar0.png")3.snapshot-pyppeteer
snapshot-pyppeteer 是 pyecharts + pyppeteer 渲染圖片的擴展,需要先安裝 pyppeteer 和 Chromium。
# 安裝
pip install snapshot-pyppeteer
# 安裝完后建議執(zhí)行 chromium 安裝命令
pyppeteer-install
# 使用方式
from snapshot_pyppeteer import snapshot
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.render import make_snapshot
def bar_base() -> Bar:
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副標題"))
)
make_snapshot(snapshot, c.render(), "bar.png")
if __name__ == '__main__':
bar_base()到此這篇關于Pyecharts可視化圖片渲染的方法詳解的文章就介紹到這了,更多相關Pyecharts可視化圖片渲染內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
CentOS6.5設置Django開發(fā)環(huán)境
這篇文章主要為大家詳細介紹了CentOS6.5設置Django開發(fā)環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Python基于Pymssql模塊實現(xiàn)連接SQL Server數(shù)據庫的方法詳解
這篇文章主要介紹了Python基于Pymssql模塊實現(xiàn)連接SQL Server數(shù)據庫的方法,較為詳細的分析了pymssql模塊的下載、安裝及連接、操作SQL Server數(shù)據庫的相關實現(xiàn)技巧,需要的朋友可以參考下2017-07-07
Python實現(xiàn)數(shù)據可視化大屏布局的示例詳解
數(shù)據可視化大屏展示需求無疑是對數(shù)據分析結果最好的詮釋,能夠使得別人能夠輕松的就理解我們的數(shù)據意圖。本文將通過pyecharts模塊來實現(xiàn),感興趣的可以了解一下2022-11-11

