#python小例子# 绘制轮播图​

2020-02-12 16:12:42 浏览数 (1)

今日分享

绘制时间线轮播图

7种水果在2个超市近10年的销量数据如下所示:

代码语言:javascript复制
{'x': ['草莓', '芒果', '葡萄', '雪梨', '西瓜', '柠檬', '车厘子'],
'沃尔玛':
{2010: [229, 538, 498, 694, 249,444, 282],
2011: [565, 237, 834, 394, 577, 265, 901],
2012: [758, 695, 114, 785, 703, 152, 193],
2013: [505,445, 344, 304, 359, 278, 302],
2014: [620, 887, 396, 282, 104, 194, 511],
2015: [210, 303, 588, 873, 901, 553, 487],
2016: [972, 975, 840, 725, 320, 921, 628],
2017: [259, 431, 382, 203, 724, 792, 833],
2018: [674, 834, 710, 391, 133, 760, 130],
2019: [575, 912, 631, 745, 297, 516, 866]},
'家乐福':
{2010: [451, 954, 769, 803, 656, 992, 413],
2011: [625, 555, 846, 655, 560, 246, 381],
2012: [484, 729, 870, 732, 475, 368, 600],
2013:[173, 121, 973, 195, 704, 116, 527],
2014: [130, 250, 853, 902, 593, 363, 200],
2015: [637, 968, 585, 657, 773, 549, 199],
2016: [952, 973, 651, 726, 631, 760, 405],
2017: [150, 463, 984, 674, 229, 159, 315],
2018: [717, 165, 889, 888, 460, 708, 180],
2019: [962, 854, 926, 863, 565, 480, 239]}}

使用pyecharts模块的timeline可按照近10年的时间线轮播2个超市7种水果的柱状图显示:

代码语言:javascript复制
from pyecharts import options as opts
from pyecharts.charts import Bar, Page, Pie, Timeline
from random import randint

data = {'x': ['草莓', '芒果', '葡萄', '雪梨', '西瓜', '柠檬', '车厘子'],
        '沃尔玛': dict(zip(range(2010, 2020), [[randint(100, 1000) for fruit in range(7)] for year in range(10)])),
        '家乐福': dict(zip(range(2010, 2020), [[randint(100, 1000) for fruit in range(7)] for year in range(10)]))
        }

print(data)


def timeline_bar() -> Timeline:
    x = data['x']
    tl = Timeline()
    for i in range(2010, 2020):
        bar = (
            Bar()
            .add_xaxis(x)
            .add_yaxis('沃尔玛', data['沃尔玛'][i])
            .add_yaxis('家乐福', data['家乐福'][i])
            .set_global_opts(title_opts=opts.TitleOpts("{}年营业额".format(i)))
        )
        tl.add(bar, "{}年".format(i))
    return tl


timeline_bar().render("./img/timeline_bar.html")

绘制的轮播图效果:


0 人点赞