1. 安装phantomjs
- pip install snapshot-phantomjs
- 下载phantomjs安装包,参见:https://phantomjs.org/download.html,不同系统下载不同的包
- 将下载下来的包解压后的bin目录下的二进制文件放到你的
$PATH中,查看$PATH的方式:echo $PATH,这里我是放到了/usr/local/bin下
cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs && chmod +x /usr/local/bin/phantomjs
2. 官方使用方法:
下面是官方的例子,我进行了一些小小的修改,允许执行宽和高,背景颜色,以及js的路径
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(init_opts=opts.InitOpts(width=width, height=height, bg_color="white", js_host=file_path))
.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])

本文详细介绍了在使用PhantomJS和PyEcharts将Echarts图表转换为图片时遇到的一系列问题及其解决方案,包括图片过大、容器部署时的错误、字体缺失、远程JS引用问题以及背景颜色设置。通过压缩图片、环境变量配置、安装字体、本地化JS引用和修改图表配置等方法,成功解决了所有问题。
最低0.47元/天 解锁文章





