pyecharts 安装
pip(pip3) install pyecharts
pyecharts 使用
官方教程
pyecharts 渲染图片
pyecharts 提供了 selenium, phantomjs 和 pyppeteer 三种方式。
笔者只用过phantomjs,其他的可以参考官方教程
引入
1 | from pyecharts.render import make_snapshot |
API
1 | def make_snapshot( |
snapshot-phantomjs 安装
snapshot-phantomjs 是 pyecharts + phantomjs 渲染图片的扩展,需要先安装 phantomjs,安装方法可以参照官网
安装
phantomjssudo apt install phantomjs或者下载 nodejs ,通过 npm 安装
npm install -g phantomjs-prebuilt安装
snapshot-phantomjspip(pip3) install snapshot-phantomjs
使用
此处采取官方示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19from 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")
使用 pyecharts 过程中遇到的问题
pyecharts 处理 pandas 数据图像不显示
原因在于数据没有被 list 处理
错误示例
1
2line_e.add_xaxis(data.index)
line_e.add_yaxis(series_name='error', y_axis=data['y_data'])正确示例
1
2line_r.add_xaxis(data.index.tolist())
line_r.add_yaxis(series_name='right', y_axis=data['y_data'].tolist())对数据进行
.list()将其列表化就可以显示图像了
make_snapshot 报错
- 渲染引擎未安装
- render 保存路径不能含有中文