一、说明
Echarts 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,pyecharts 诞生了。
二、官方参考网址
pyecharts基本实用参考:
https://pyecharts.org/#/zh-cn/
pyecharts代码效果参考:
https://gallery.pyecharts.org/#/Bar/bar_with_brush
三、组合组件
1、Grid组件
首先介绍Pyecharts模块当中的Grid组件,使用Grid组件可以很好地将多张图无论是上下组合还是左右组合,都能够很好地拼接起来,我们先来看第一个例子
上下组合:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# time : 2022/2/25 0025
from pyecharts.charts import Bar,Line,Grid
from pyecharts import options as opt
import random
cate = ['1月', '2月', '3月', '4月', '5月', '6月','7月','8月','9月','10月','11月','12月']
bar = (
Bar()
.add_xaxis(cate)
.add_yaxis('月订单数',[random.randint(100,200) for _ in cate])
.add_yaxis('月订单完成数',[random.randint(50,100) for _ in cate])
.set_global_opts(title_opts = opt.TitleOpts(title='直方图'))
)
bar.render('bar.html')
lines = (
Line()
.add_xaxis(cate)
.add_yaxis('月订单数',[random.randint(100,200) for _ in cate])
.add_yaxis('月订单完成数',[random.randint(50,100) for _ in cate])
.set_global_opts(title_opts=opt.TitleOpts(title='折线图',pos_top='48%'),
legend_opts=opt.LegendOpts(pos_top = '48%'))
)
lines.render('lines.html')
grid = (
Grid()
.add(bar,
grid_opts=opt.GridOpts(pos_bottom='60%'))
.add(lines,grid_opts=opt.GridOpts(pos_top='50%'))
.render('水平组合图.html')
)
左右组合:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# time : 2022/2/25 0025
from pyecharts.charts import Bar,Line,Grid
from pyecharts import options as opt
import random
cate = ['1月', '2月', '3月', '4月', '5月', '6月','7月','8月','9月','10月','11月','12月']
bar = (
Bar()
.add_xaxis(cate)
.add_yaxis('月订单数',[random.randint(100,200) for _ in cate])
.add_yaxis('月订单完成数',[random.randint(50,100) for _ in cate])
.set_global_opts(title_opts = opt.TitleOpts(title='直方图'),
legend_opts=opt.LegendOpts(pos_left='20%'))
)
bar.render('bar.html')
lines = (
Line()
.add_xaxis(cate)
.add_yaxis('月订单数',[random.randint(100,200) for _ in cate])
.add_yaxis('月订单完成数',[random.randint(50,100) for _ in cate])
.set_global_opts(title_opts=opt.TitleOpts(title='折线图',pos_right='5%'),
legend_opts=opt.LegendOpts(pos_right='20%')
)
)
grid2 = (
Grid()
.add(bar, grid_opts=opt.GridOpts(pos_left="60%"))
.add(lines, grid_opts=opt.GridOpts(pos_right="50%"))
.render("垂直组合图.html")
)
可以看到我们无论是想上下拼接还是左右拼接,都可以通过调整参数“pos_left”、“pos_right”、“pos_top”以及“pos_bottom”这几个参数来实现。
地图和折线图拼接
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# time : 2022/2/25 0025
from pyecharts.charts import Bar,Line,Grid,Map
from pyecharts import options as opt
import random
cate = ['1月', '2月', '3月', '4月', '5月', '6月','7月','8月','9月','10月','11月','12月']
lines = (
Line()
.add_xaxis(cate)
.add_yaxis('月订单数',[random.randint(100,200) for _ in cate])
.add_yaxis('月订单完成数',[random.randint(50,100) for _ in cate])
.set_global_opts(title_opts=opt.TitleO