10月 14

python用echarts生成图表

在Centos7 64位下python2.7.13需要生成图表,开始想使用matplotlib。但是安装后出现问题。
最后决定用echarts生成图表再转成图片。这个想法找到了一个方便的库,详细记录如下。

安装相关库
pip install pyecharts
pip install pyecharts-snapshot

安装html转图片、pdf工具
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
mv phantomjs-2.1.1-linux-x86_64 /usr/local/src/phantomjs
ln -sf /usr/local/src/phantomjs/bin/phantomjs /usr/local/bin/phantomjs

防止phantomjs转图片乱码,安装字体。
在centos中执行:yum install bitmap-fonts bitmap-fonts-cjk
在ubuntu中执行:sudo apt-get install xfonts-wqy

官方脚本修改如下

#!/bin/env python
# -*- coding:utf-8 -*-

from __future__ import unicode_literals

from pyecharts import Line, Pie, Grid
from pyecharts_snapshot.main import make_a_snapshot

attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 10, 100]
v2 = [55, 60, 16, 20, 15, 80]
line = Line("折线图示例")
line.add("商家A", attr, v1, mark_point=["average"])
line.add("商家B", attr, v2, is_smooth=True, mark_line=["max", "average"])
line.render('test.html')

make_a_snapshot('test.html', 'test.jpeg')

通过实验已经成功,但是还有些小问题未解决。

官方文档如下
https://github.com/chenjiandongx/pyecharts/blob/master/docs/zh-cn/documentation.md#%E5%BC%80%E5%A7%8B%E4%BD%BF