PyEcharts 数据可视化

1.0 Bar 柱状图

from pyecharts.charts import Bar

bar = Bar()
bar.add_xaxis(["衬衣", "羊毛衫", "西装", "裤子", "鞋子", "袜子"])
bar.add_yaxis("商家", [5, 20, 40, 10, 70, 90])
# 相当于
bar = (
Bar()
.add_xaxis(["衬衣", "羊毛衫", "西装", "裤子", "鞋子", "袜子"])
.add_yaxis("商家", [5, 20, 40, 10, 70, 90])
.add_yaxis("商家B", [15, 6, 45, 20, 35, 66]) # 支持两个柱子同时生成
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题")) # 生成标题
)


bar.render("bar.html") # 渲染为 bar.html,默认是 render.html
  • 效果
Awesome-pyecharts
  • 生成图片
from snapshot_selenium import snapshot as driver
from pyecharts.render import make_snapshot
def bar_chart() -> Bar:
bar = (
Bar()
.add_xaxis(["衬衣", "羊毛衫", "西装", "裤子", "鞋子", "袜子"])
.add_yaxis("商家", [5, 20, 40, 10, 70, 90])
)
return bar

# 需要安装 snapshot-selenium 或者 snapshot-phantomjs
make_snapshot(driver, bar_chart().render(), "bar.png")
# 调用函数bar_chart()进行生成
# 可以直接使用bar.render(),其中bar=Bar()

2.0 饼图