pyecharts(2)图的布局

图的布局

from pyecharts.charts import *
from pyecharts.components import Table
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
import random
import datetime
import math
import numpy as np 

from pyecharts.globals import CurrentConfig
CurrentConfig.ONLINE_HOST = "https://cdn.kesci.com/lib/pyecharts_assets/" # 设置host地址

时序控制

begin = datetime.date(2020, 4, 1)
end = datetime.date(2020, 4, 20)
cate = ['Apple', 'Huawei', 'MI', 'Oppo', 'Vivo', 'Samsung']

def random_data(n):
    return [random.randint(100, 200) for i in range(n)]

tl = Timeline()
tl.add_schema()

for i in range((end-begin).days + 1):
    day = begin + datetime.timedelta(days=i)
    
    bar = (
        Bar()
        .add_xaxis(cate)
        .add_yaxis('电商渠道', random_data(len(cate)))
    )
    tl.add(bar, day)
    
tl.render_notebook()

按钮控制

cate = ['Apple', 'Huawei', 'MI', 'Oppo', 'Vivo', 'Samsung']
begin = datetime.date(2020, 4, 1)
end = datetime.date(2020, 4, 20)
date_list = [str(begin + datetime.timedelta(days=i))
             for i in range((end - begin).days + 1)]

def random_data(n):
    return [random.randint(0, 100) for i in range(n)]

'''按钮'''
tab = Tab()
for c in cate:
    day = begin + datetime.timedelta(days=i)
    
    line = (
        Line()
        .add_xaxis(date_list)
        .add_yaxis('', random_data(len(date_list)))
    )
    tab.add(line, c)
tab.render_notebook()

顺序排布

x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]

bar = (
    Bar()
    .add_xaxis(x_data)
    .add_yaxis('', y_data)
)
line = (Line()
        .add_xaxis(x_data)
        .add_yaxis('', y_data)
        )
'''顺序多图'''
page = Page()
page.add(bar, line)
page.render_notebook()

网格排布

x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]


bar = (Bar()
       .add_xaxis(x_data)
       .add_yaxis('', y_data)
       )

line = (Line()
        .add_xaxis(x_data)
        .add_yaxis('', y_data)
        )

'''网格多图'''
grid = (
    Grid()
    .add(bar, grid_opts=opts.GridOpts(pos_left='60%'))
    .add(line, grid_opts=opts.GridOpts(pos_right='60%'))
)
grid.render_notebook()

posted @ 2020-08-19 21:24  hichens  阅读(2777)  评论(1编辑  收藏  举报