pyecharts 定制主題

2023-02-17 11:11 更新
pyecharts 內置提供了 10+ 種不同的風格,另外也提供了便捷的定制主題的方法。

默認主題的效果

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType

def theme_default() -> Bar:
    c = (
        Bar()
        # 等價于 Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
        .add_xaxis(Faker.choose())
        .add_yaxis("商家A", Faker.values())
        .add_yaxis("商家B", Faker.values())
        .add_yaxis("商家C", Faker.values())
        .add_yaxis("商家D", Faker.values())
        .set_global_opts(title_opts=opts.TitleOpts("Theme-default"))
    )
    return c

主題風格

LIGHT

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))

DARK

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))

CHALK

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.CHALK))

ESSOS

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.ESSOS))

INFOGRAPHIC

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.INFOGRAPHIC))

MACARONS

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))

PURPLE_PASSION

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))

ROMA

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMA))

ROMANTIC

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMANTIC))

SHINE

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.SHINE))

VINTAGE

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))

WALDEN

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.WALDEN))

WESTEROS

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.WESTEROS))

WONDERLAND

c = Bar(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND))

使用自己構建的主題

Echarts 提供了主題構建工具,你可以從中構建喜歡的主題,假設你構建的主題文件為 ?myTheme.js?。

使用自己構建的主題必須由開發(fā)者自己啟動資源文件服務器,這部分內容請參考 進階話題-資源引用,然后你需要進行以下步驟的操作

  1. 將 ?myTheme.js? 放入至 ?pyecharts-assets/assets/themes? 文件夾。
  2. 注冊主題到 pyecharts
     from pyecharts.datasets import register_files
     register_files({"myTheme": ["themes/myTheme", "js"]})
  3. 使用主題
    c = Bar(init_opts=opts.InitOpts(theme="myTheme"))


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號