pyecharts画图

资源

环境

代码

柱状图:各门店销量真实值、预测值、误差值总览

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar

def bar_base(df) -> Bar:
    c = Bar()
    for store in df['Store'].unique():
        _df = df[df['Store']==store]
        ds = _df['ds'].astype(str).tolist()
        ys = _df['y'].astype(float).tolist()
        yhats = _df['yhat'].astype(float).tolist()
        yerrors = _df['error'].astype(float).tolist()
        c.add_xaxis(ds)
        c.add_yaxis(store, ys)
        c.add_yaxis(store+' Predict', yhats)
        c.add_yaxis(store+' Error', yerrors)
    c.set_global_opts(title_opts=opts.TitleOpts(title=''),\
                         datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=100))
    c.set_series_opts(label_opts = opts.LabelOpts(is_show=False))
    return c

bar_base(analyse_df).render_notebook()

饼图:误差大于0.8的记录数量对比,以weekday维度聚合

from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Pie

def df_report(df, groupby_col, name, f):
    report_df = pd.DataFrame(columns=[groupby_col, name])
    i = 0
    for groupby_value in df[groupby_col].unique():
        _df = df[df[groupby_col]==groupby_value]
        error_count = f(_df)
        report_df.loc[i] = [groupby_value, error_count]
        i += 1
    return report_df.sort_values(by=name, ascending=False)

def pie_report(df, groupby_col, name, f) -> Pie:
    report_df = df_report(df, groupby_col, name, f)
    c = (
        Pie()
        .add("",
             [list(z) for z in zip(report_df[groupby_col], report_df[name])],
            radius=["30%", "75%"],
            center=["75%", "50%"],
            rosetype="area",
            )
        .set_global_opts(title_opts=opts.TitleOpts(title=name),
                         legend_opts=opts.LegendOpts(type_="scroll", pos_left="0%", pos_top="15%", orient="vertical"),)
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    return c.render_notebook()

def pie_reports(df, groupby_col, name, fs) -> Pie:
    c = Pie()
    i = 0.5
    n = len(fs)
    for f in fs:
        report_df = df_report(df, groupby_col, name, f)
        c.add("",
                 [list(z) for z in zip(report_df[groupby_col], report_df[name])],
                radius=["30%", "75%"],
                center=[f"{i/n*100}%", "50%"],
                rosetype="area",
                )
        i += 1
    c.set_global_opts(title_opts=opts.TitleOpts(title=name))
#     c.set_global_opts(title_opts=opts.TitleOpts(title=name),
#                       legend_opts=opts.LegendOpts(type_="scroll", pos_left="0%", pos_top="15%", orient="vertical"),)
#     c.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    return c.render_notebook()

pie_report(df, 'weekday', 'Count', lambda x: len(x[x['error_p']>0.8]))
pie_reports(df, 'weekday', 'Count', [lambda x: len(x[x['error_p']>0.8]), lambda x: len(x[x['error']>800])])

笔记

  1. 不关心v0.5.x,只关心v1系列(从v1.0.0开始),v1仅支持python3.6+,且与v0.5.x不兼容。
  2. 支持notebook环境、主流web框架,有30+种常见图表、简洁的api。
  3. jupyter notebook可以直接渲染--基本图表notebook例子,jupyter lab需要配置
  4. 最常用的柱状图、折线图:直角坐标系图表
  5. 参数的XXXOpts/XXXItems和dict形式是完全等价的,即下列形式:
    • c = Bar(init_opts=opts.InitOpts(width="620px", height="420px"))
    • c = Bar(dict(width="620px", height="420px"))
    • c = Bar({"width": "620px", "height": "420px"})
  6. 数据本质上只是将Python转换为JSON,故第三方格式如numpy.intXX需自行转换为Python原生数据格式:
    • 方法1:
      • [int(x) for x in your_numpy_array_or_something_else]
      • [float(x) for x in your_numpy_array_or_something_else]
      • [str(x) for x in your_numpy_array_or_something_else]
    • 方法2:
      • Series.tolist()
  7. 10+种内置主题,亦可自行定制主题
  8. 渲染成图片有seleniumphantomjspyppeteer三种方式
  9. 支持传入原生JS函数
  10. pyecharts 使用的所有静态资源文件存放于 pyecharts-assets 项目中,默认挂载在 https://assets.pyecharts.org/assets/
  11. 支持整合在Web框架中,包括:
    • Flask
    • Sanic
    • Django
    • Tornado
  12. pyecharts 内置了一些常用的城市地理坐标数据,这些数据保存在 pyecharts/datasets/city_coordinates.json 文件中。格式可描述为以下形式:

Page created on 2020-12-06