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()
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])])
-
不关心v0.5.x,只关心v1系列(从v1.0.0开始),v1仅支持python3.6+,且与v0.5.x不兼容。
-
支持notebook环境、主流web框架,有30+种常见图表、简洁的api。
-
jupyter notebook可以直接渲染--基本图表notebook例子,jupyter lab需要配置
-
最常用的柱状图、折线图:直角坐标系图表
-
参数的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"})
-
数据本质上只是将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:
-
10+种内置主题,亦可自行定制主题
-
渲染成图片有
selenium
、phantomjs
、pyppeteer
三种方式
-
支持传入原生JS函数
-
pyecharts 使用的所有静态资源文件存放于 pyecharts-assets 项目中,默认挂载在 https://assets.pyecharts.org/assets/
-
支持整合在Web框架中,包括:
-
Flask
-
Sanic
-
Django
-
Tornado
-
pyecharts 内置了一些常用的城市地理坐标数据,这些数据保存在 pyecharts/datasets/city_coordinates.json 文件中。格式可描述为以下形式: