做图工具 pyecharts

1. 说明

  上次分享了 Flask+Dash 做图,WXXCX 师兄给我留言说:感觉 dash 不如 pyecharts 好用,于是我学习了一下 pyecharts。

 ECharts,缩写来自 Enterprise Charts,商业级数据图表,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11,chrome,firefox,Safari 等)。Pyecharts 是 python 版本的 echarts,与 Dash 相比,我个人更喜欢它的图片配色;在使用上它相似于 matplotlib,不需要像 Dash 一样再去熟悉新的 API 和 callback 的逻辑;最喜欢的地方在于 notebook 可以调,flask 也可以调,调试时和显示在网页上的图完全一样;它还支持地图显示,以及雷达图等等(Dash 是否支持我没试过)。果然更加好用,整理如下。

2. 准备数据

1
2
3
4
5
import pyecharts

attr = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]

3. 柱图

1
2
3
4
5
bar = pyecharts.Bar("Title1", "Title2")
bar.add("v1", attr, v1, mark_line=["average"], mark_point=["max", "min"])
bar.add("v2", attr, v2, mark_line=["average"], mark_point=["max", "min"])
bar.render('test.html')
bar
image.png

4. 直方图

1
2
3
4
bar = pyecharts.Bar('Title1', 'Title2')
bar.add(name = 'v1', x_axis = attr, y_axis = v1, bar_category_gap = 0)
bar.render('test.html')
bar

5. 堆叠柱图

1
2
3
4
5
bar = pyecharts.Bar("Title1", "Title2") 
bar.add('v1',attr,v1,is_stack = True) # is_stack = True才表示堆叠在一起
bar.add('v2',attr,v2,is_stack = True)
bar.render('test.html')
bar

6. 散点图

1
2
3
4
5
6
scatter = pyecharts.Scatter('Title1', 'Title2')
x = [i for i in range(0, len(attr))]
scatter.add("v1", x, v1)
scatter.add("v2", x, v2)
scatter.render('test.html')
scatter

7. 特效散点图

1
2
3
4
5
es = pyecharts.EffectScatter("Title1", "Title2")
es.add("v1", range(0, len(attr)), v1, legend_pos='center',
effect_period=3, effect_scale=3.5, symbol='pin', is_label_show=True)
es.render("test.html")
es

8. 折线图

1
2
3
4
5
line = pyecharts.Line("Title1", "Title2")
line.add("v1", attr, v1, mark_point=['average'])
line.add("v2", attr, v2, mark_line=['average'], is_smooth=True)
line.render('test.html')
line

9. 饼图

1
2
3
4
5
pie = pyecharts.Pie("Title1", "Title2")
pie.add('v1', attr, v1, is_label_show=True, legend_pos='right',
label_text_color=None, legend_orient='vertical', radius=[30, 75])
pie.render('test.html')
pie

10. 箱图

1
2
3
4
5
6
7
boxplot = pyecharts.Boxplot('Title1', 'Title2')
x_axis = ['v1','v2']
y_axis = [v1, v2]
yaxis = boxplot.prepare_data(y_axis)
boxplot.add("value", x_axis, y_axis)
boxplot.render('test.html')
boxplot

11. 多种类型图叠加

1
2
3
4
5
6
7
8
9
bar = pyecharts.Bar('Title1', 'Title2') 
bar.add('v1',attr,v1)
line = pyecharts.Line()
line.add('v2',attr,v2)
overlop = pyecharts.Overlap()
overlop.add(bar)
overlop.add(line)
overlop.render('test.html')
overlop

12. 在网页中显示图表

  与 flask 框架结合,pythechart 将图存成网页,再用 flask 显示该网页,注意运行前先建立 templates 目录,flask 默认从该目录读取网页。如果运行以下程序没有问题,则在浏览器打开 http://localhost:9993 即可看到图片。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from flask import Flask
from sklearn.externals import joblib
from flask import Flask,render_template,url_for
import pyecharts

server = Flask(__name__)

def render_test_1():
attr = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
line = pyecharts.Line("Title1", "Title2")
line.add("v1", attr, v1, mark_point=['average'])
line.add("v2", attr, v2, mark_line=['average'], is_smooth=True)
line.render('templates/bar01.html')

@server.route('/')
def do_main():
render_test_1()
return render_template('bar01.html')

if __name__ == '__main__':
server.run(debug=True, port=9993, host="0.0.0.0")