python 与echart结合

2019-06-27  本文已影响0人  噗嗤噗哩噗通

前言
为了搞可视化且自动化的报表,节约自己的搬砖成本,于是来尝试一下。
折腾了一波发现是这样的:
如果说想要达到自动化报表的炫酷,且定时刷新,pyechart比较好用,要不然基本的作图工具差不多能够支持


1. 安装

由于本地只有python3,以下是基于python3的
pyecharts的0.5版本和1.0版本差距挺大,建议还是安装1.0版本

pip3 install pyecharts
pip3 install wheel
pip3 install pyecharts_snapshot #只支持python3
pip3 install echarts-themes-pypkg

2. 简单建立图:

首先是官网资源(里面很多代码是结合js框架来做调用,所以和实际撰写存在偏差):
https://pyecharts.org/#/zh-cn/rectangular_charts?id=scatter%ef%bc%9a%e6%95%a3%e7%82%b9%e5%9b%be

github官网位置:
https://github.com/pyecharts/pyecharts

参数对应数据:参数比较全
https://blog.csdn.net/miner_zhu/article/details/81949004

由于例子官网很多,我就整理我自己撰写的部分:

from pyecharts.charts import Bar, Grid, Line,Scatter
# 1.0版本是从pyechart.charts 导入,0.5版本是直接导入
#from pyecharts import Grid
# from pyecharts import WordCloud
# from pyecharts import Pie
# from pyecharts import Map
import pandas as pd 
from pyecharts.charts import Page
import pyecharts.options as opts
from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts
# 设置全局主题,暂时在主题设置上还有一些疑问
from pyecharts.globals import ThemeType
page=Page()
page.theme = ThemeType.PURPLE_PASSION

attr = ['Jan','Feb','Mar','Apr','May']
value1 = [ 23,34,45,34,34]

# 画第一条线:
line = Line()
line.add_xaxis(attr)
line.add_yaxis(
            "app",
            value1,
            markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]),
        )
line.set_global_opts(title_opts=opts.TitleOpts(title="图1"))
# 图2
attr = ['Jan','Feb','Mar','Apr','May']
value1 = [ 23,34,45,34,34]
line1 = Line()
line1.add_xaxis(attr)
line1.add_yaxis(
            "app",
            value1,
            markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]),
        )
line1.set_global_opts(title_opts=opts.TitleOpts(title="图2", pos_right="5%"))
# 两个图合并到同一个视图中
grid = Grid()    
grid.add(line, grid_opts=opts.GridOpts(pos_left='60%'))       
grid.add(line1, grid_opts=opts.GridOpts(pos_right='60%'))  

# 图3,这里的tmp_1.cnt和tmp_1.user为数据,label-t为分类标签
from pyecharts.charts import Scatter
sca_1=Scatter()
sca_1.add_xaxis(tmp_1.cnt)
sca_1.add_yaxis(
            "app",
            [list(z) for z in zip(tmp_1.user, tmp_1.label_t)],
            label_opts=opts.LabelOpts(formatter="")
        )
sca_1.set_global_opts(
            title_opts=opts.TitleOpts(title="Scatter-多维度数据",subtitle="副标题"),
            tooltip_opts=opts.TooltipOpts(formatter="{c}"),
            visualmap_opts=opts.VisualMapOpts(
                type_="color", max_=90, min_=0, dimension=2
             ),
        )

# 输出
page.add(grid)
page.add(sca_1)
page.render('page.html') 

上一篇 下一篇

猜你喜欢

热点阅读