import pandas as pd
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
from pandasecharts import echart
products = Faker.choose()
dfs = []
for year in (2012, 2013, 2014, 2015):
dfs.append(
pd.DataFrame(
zip(products, Faker.values(), Faker.values(), [year]*len(products)),
columns=["商品", "商家A", "商家B", "年份"]
)
)
df = pd.concat(dfs, axis=0)
df.head()
| 商品 | 商家A | 商家B | 年份 | |
|---|---|---|---|---|
| 0 | 河马 | 111 | 62 | 2012 |
| 1 | 蟒蛇 | 128 | 89 | 2012 |
| 2 | 老虎 | 70 | 47 | 2012 |
| 3 | 大象 | 143 | 148 | 2012 |
| 4 | 兔子 | 38 | 66 | 2012 |
显示2012年商品在商家A的销量
df.query("年份==2012").echart.bar("商品", "商家A").render_notebook()
坐标值反转
df.query("年份==2012").echart.bar("商品", "商家A", reverse_axis=True).render_notebook()
显示2012年商品在所有商家的销量
df.query("年份==2012").echart.bar("商品", ["商家A", "商家B"]).render_notebook()
堆叠显示
df.query("年份==2012").echart.bar("商品", ["商家A", "商家B"], stack_view=True).render_notebook()
设置x轴,y轴名称以及标题和副标题
df.query("年份==2012").echart.bar("商品", ["商家A", "商家B"],
xaxis_name="Product",
yaxis_name="sales",
title="Product sales count in 2012",
subtitle="The subtitle").render_notebook()
label设置
df.query("年份==2012").echart.bar("商品", ["商家A", "商家B"],
label_show=True).render_notebook()
df.query("年份==2012").echart.bar("商品", ["商家A", "商家B"],
label_show=True,
label_opts={"formatter": "{b}:{c}"}).render_notebook()
df.query("年份==2012").echart.bar("商品", ["商家A", "商家B"],
label_show=True,
reverse_axis=True,
label_opts={"position": "right"}).render_notebook()
legend设置
df.query("年份==2012").echart.bar("商品", ["商家A", "商家B"],
legend_opts={"is_show": True, "pos_left": "left"}
).render_notebook()
主题设置
df.query("年份==2012").echart.bar("商品", ["商家A", "商家B"],
theme=ThemeType.LIGHT,
).render_notebook()
所有年份的总销量/平均销量
df.echart.bar("商品", ["商家A", "商家B"],
agg_func="sum",
).render_notebook()
df.echart.bar("商品", ["商家A", "商家B"],
agg_func="mean",
).render_notebook()
按照年份显示多张图表
df.echart.bar("商品", ["商家A", "商家B"],
by="年份",
).render_notebook()
以时间轴显示多个年份
df.echart.bar("商品", ["商家A", "商家B"],
timeline="年份",
).render_notebook()
时间轴动态排序
df.echart.bar("商品", "商家A",
timeline="年份",
reverse_axis=True,
sort="商家A",
label_show=True,
timeline_opts={"is_auto_play": True}
).render_notebook()