共计 2710 个字符,预计需要花费 7 分钟才能阅读完成。
安装命令:pip install pyecharts
pyecharts 官网:https://pyecharts.org/
5 分钟上手
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis([" 衬衫 ", " 羊毛衫 ", " 雪纺衫 ", " 裤子 ", " 高跟鞋 ", " 袜子 "])
bar.add_yaxis(" 商家 A ", [5, 20, 36, 10, 75, 90])
# render:默认在当前目录生成 render.html 文件,也可传入路径参数,如 bar.render("mycharts.html")
bar.render()
# pyecharts 所有方法均支持链式调用
bar = (Bar()
.add_xaxis([" 衬衫 ", " 羊毛衫 ", " 雪纺衫 ", " 裤子 ", " 高跟鞋 ", " 袜子 "])
.add_yaxis(" 商家 A ", [5, 20, 36, 10, 75, 90])
)
options 配置项
在 pyecharts 中,一切皆 Options。
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (Bar()
.add_xaxis([" 衬衫 ", " 羊毛衫 ", " 雪纺衫 ", " 裤子 ", " 高跟鞋 ", " 袜子 "])
.add_yaxis(" 商家 A ", [5, 20, 36, 10, 75, 90])
.set_global_opts(title_opts={"text": " 主标题 ", "subtext": " 副标题 "})
)
渲染成图片
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
# 使用 snapshot-selenium 渲染图片:pip install snapshot_selenium
from snapshot_selenium import snapshot
bar = (Bar()
.add_xaxis([" 衬衫 ", " 羊毛衫 ", " 雪纺衫 ", " 裤子 ", " 高跟鞋 ", " 袜子 "])
.add_yaxis(" 商家 A ", [5, 20, 36, 10, 75, 90])
)
make_snapshot(snapshot, bar.render(), "bar.png")
使用主题
from pyecharts import options as opts
from pyecharts.globals import ThemeType # 导入主题包
bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis([" 衬衫 ", " 羊毛衫 ", " 雪纺衫 ", " 裤子 ", " 高跟鞋 ", " 袜子 "])
.add_yaxis(" 商家 A ", [5, 20, 36, 10, 75, 90])
.add_yaxis(" 商家 B ", [15, 6, 45, 20, 35, 66])
)
bar.render()
pyecharts 内置主题:https://pyecharts.org/#/zh-cn/themes
配置项
全局配置项
全局配置项通过 set_global_opts
方法设置。
初始化配置项
from pyecharts.charts import Bar, Line
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType, RenderType
bar = (
Bar(
init_opts=opts.InitOpts(
width="700px", # 图表画布大小
height="400px",
page_title=" 网页标题 ",
)
)
.add_xaxis(Faker.choose())
.add_yaxis(" 商家 A ", Faker.values())
.add_yaxis(" 商家 B ", Faker.values())
)
bar.render()
标题配置项
bar = (Bar()
.add_xaxis(Faker.choose())
.add_yaxis(" 商家 A ", Faker.values())
.add_yaxis(" 商家 B ", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(
title=" 柱形图 ", # 主标题
title_link="https://aboss.top/",
title_target="blank",
subtitle=" 副标题 ",
)
)
)
区域缩放配置项
bar = (Bar()
.add_xaxis(Faker.choose())
.add_yaxis(" 商家 A ", Faker.values())
.add_yaxis(" 商家 B ", Faker.values())
.set_global_opts(
datazoom_opts=opts.DataZoomOpts(
is_show=True,
type_="slider", # 组件类型,默认 "slider", 可选 "inside"
)
)
)
系列配置项
图元样式配置项
line = (Line()
.add_xaxis(Faker.choose())
.add_yaxis(" 商家 A ", Faker.values())
.add_yaxis(" 商家 B ", Faker.values())
.set_series_opts(
itemstyle_opts=opts.ItemStyleOpts(color="red",)
)
)
原生 JavaScript
pyecharts 支持传入原生 JS 函数。
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.commons.utils import JsCode
color_func = """
function (params) {if (params.value > 0 && params.value < 50) {return 'red'} else if (params.value > 50 && params.value < 100) {return 'blue'} else {return 'green'}
}
"""
bar = (Bar()
.add_xaxis(Faker.choose())
.add_yaxis(
" 商家 A ",
Faker.values(),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_func),
),
)
)
bar.render()
正文完