最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pyecharts可視化圖片渲染的方法詳解

 更新時(shí)間:2022年02月22日 09:01:41   作者:葉庭云  
使用 pyecharts 渲染成圖片一直是開發(fā)者比較關(guān)心的功能,pyecharts提供了selenium、phantomjs和pyppeteer 三種方式。本文將具體介紹一下這三種方式的使用,需要的可以參考一下

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

更多介紹可以學(xué)習(xí)官方文檔:https://pyecharts.org/#/zh-cn/render_images

首先需要安裝上snapshot-selenium

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

測(cè)試代碼如下:

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

關(guān)鍵代碼:

from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot

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

結(jié)果如下:

補(bǔ)充

當(dāng)然Pyecharts不僅能進(jìn)行可視化圖片渲染,還能進(jìn)行圖表的渲染,同樣也是使用selenium, phantomjs 和 pyppeteer這三種方式

渲染圖片依賴庫(kù)

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,

    # 延遲時(shí)間,避免圖還沒(méi)渲染完成就生成了圖片,造成圖片不完整
    delay: float = 2,

    # 像素比例,用于調(diào)節(jié)圖片質(zhì)量
    pixel_ratio: int = 2,

    # 渲染完圖片是否刪除原 HTML 文件
    is_remove_html: bool = False,

    # 瀏覽器類型,目前僅支持 Chrome, Safari,使用 snapshot-selenium 時(shí)有效
    browser: str = "Chrome",
    **kwargs,
)

渲染方式

1.snapshot-selenium

snapshot-selenium 是 pyecharts + selenium 渲染圖片的擴(kuò)展,使用 selenium 需要配置 browser driver,這部分可以參考 selenium-python 相關(guān)介紹,推薦使用 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(["襯衫", "毛衣", "領(lǐng)帶", "褲子", "風(fēng)衣", "高跟鞋", "襪子"])
        .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-測(cè)試渲染圖片"))
    )
    return c

make_snapshot(snapshot, bar_chart().render(), "bar0.png")

2.snapshot-phantomjs

snapshot-phantomjs 是 pyecharts + phantomjs 渲染圖片的擴(kuò)展,需要先安裝 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(["襯衫", "毛衣", "領(lǐng)帶", "褲子", "風(fēng)衣", "高跟鞋", "襪子"])
        .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-測(cè)試渲染圖片"))
    )
    return c

make_snapshot(snapshot, bar_chart().render(), "bar0.png")

3.snapshot-pyppeteer

snapshot-pyppeteer 是 pyecharts + pyppeteer 渲染圖片的擴(kuò)展,需要先安裝 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="我是副標(biāo)題"))
    )
    make_snapshot(snapshot, c.render(), "bar.png")


if __name__ == '__main__':
    bar_base()

到此這篇關(guān)于Pyecharts可視化圖片渲染的方法詳解的文章就介紹到這了,更多相關(guān)Pyecharts可視化圖片渲染內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

山阳县| 黄平县| 常宁市| 象山县| 兴业县| 南充市| 宜川县| 时尚| 洛浦县| 临汾市| 河北区| 塔城市| 重庆市| 易门县| 云梦县| 延边| 衡阳市| 九台市| 威信县| 永嘉县| 策勒县| 偏关县| 随州市| 凤翔县| 沁水县| 红安县| 绥阳县| 湘西| 搜索| 安阳县| 隆回县| 江口县| 横山县| 柞水县| 兴义市| 万源市| 喜德县| 水城县| 叙永县| 开封县| 海林市|