简介:当日常工作中需要对数据进行展示,我们可以使用一个简单、易用、图形非常漂亮、并具备一定扩展能力和组合能力的统计图表库pyechart,它是基于图形语法理论搭建而成,帮助开发者以最小成本绘制高质量统计图表。
历史攻略:
安装:
pip install pyecharts -U
折线图:
柱状图:
饼图:
案例源码:
# -*- coding: utf-8 -*-
# time: 2022/10/17 10:30
# file: data_show.py
# 公众号: 玩转测试开发
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.charts import Bar, Line, Pie
def column_chart() -> Bar:
"""
柱状图
:return:
"""
c = (
Bar({"theme": ThemeType.MACARONS})
.add_xaxis(["2017", "2018", "2019", "2020", "2021"])
.add_yaxis("中国", [8816.00, 9905.00, 10143.00, 10434.00, 12551.50])
.add_yaxis("美国", [60109.00, 63064.00, 65279.00, 63413.00, 69386.40])
.set_global_opts(
title_opts={"text": "近5年中美人均GDP对比"}
)
)
return c
def line_base() -> Line:
"""
折线图
:return:
"""
attr = ["2017", "2018", "2019", "2020", "2021"]
v1 = [8816.00, 9905.00, 10143.00, 10434.00, 12551.50]
v2 = [60109.00, 63064.00, 65279.00, 63413.00, 69386.40]
c = (
Line()
.add_xaxis(attr)
.add_yaxis("中国", v1)
.add_yaxis("美国", v2)
.set_global_opts(title_opts=opts.TitleOpts(title="近5年中美人均GDP对比"))
)
return c
def pie_set_colors() -> Pie:
"""
饼状图
:return:
"""
c = (
Pie()
.add("", [list(z) for z in zip(['唱', '跳', 'rap', '篮球'], [2, 3, 4, 5])])
# .set_colors(["red", "blue", "green", "orange"])
.set_global_opts(title_opts=opts.TitleOpts(title="小黑子"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
return c
if __name__ == '__main__':
column_chart().render("my_column_chart.html")
line_base().render("my_line_base.html")
pie_set_colors().render("my_pie.html")