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. 准备数据
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. 柱图
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. 直方图
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. 堆叠柱图
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. 散点图
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. 特效散点图
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. 折线图
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. 饼图
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. 箱图
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. 多种类型图叠加
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 即可看到图片。
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")
网友评论