8.pyecharts日历图

一、适用条件
1、日历图:查看整体数据,细化到每一天,自定义时间。
二、代码实现
1.导入所需包

import datetime
import random
from pyecharts import options as opts
from pyecharts.charts import Calendar
import pandas as pd
import numpy as np 
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
#from snapshot_phantomjs import snapshot
from snapshot_pyppeteer import snapshot
# from datetime import datetime ###自己导入数据时,可以转换成str格式

2.数据整理

##########自己导入数据时可参考
# df = pd.read_excel('picture.xlsx',sheet_name='calendar')
# data = [list(z) for z in zip(df["日期"].dt.strftime('%Y-%m-%d'),list(df["销量"]))]
#########
begin = datetime.date(2020, 1, 1)
end = datetime.date(2020, 12, 31)
data = [
        [str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)]
        for i in range((end - begin).days + 1)
    ]   

3 .日历图

def Calendar_chart() -> Calendar:
    ################## 这部分可以直接用,保存成网页

    c = (
        Calendar()
        .add(
            "",
            data, ###传入数据
            calendar_opts=opts.CalendarOpts(
                range_=["2020-01-01","2020-12-31"],###设置日期范围
                daylabel_opts=opts.CalendarDayLabelOpts(name_map="cn"),
                monthlabel_opts=opts.CalendarMonthLabelOpts(name_map="cn"),
            ),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Calendar-2020年微信步数情况(中文 Label)"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
            visualmap_opts=opts.VisualMapOpts(
                max_=25000,
                min_=1000,
                orient="horizontal",
                is_piecewise=False,
                pos_top="230px",
                pos_left="100px",
            ),
        )
        # .render("1.html")
    )
    return c
make_snapshot(snapshot, Calendar_chart().render(), "8_1.gif")
if __name__ == '__main__':
    Calendar_chart()
8_1.gif

你可能感兴趣的:(8.pyecharts日历图)