echarts 是百度开源的一个数据可视化 JS 库,主要用于数据可视化。pyecharts 是一个用于生成 Echarts 图表的类库。实际上就是 Echarts 与 Python 的对接。pyecharts分为v0.5和v1两个版本,v0.5和v1两个版本并不兼容,v1是一个船新的版本,本文绘图均基于v1版本。
echarts官网: https://www.echartsjs.com/index.html
pyecharts官网:https://pyecharts.org/#/zh-cn/intro
from pyecharts.charts import 函数名
pyecharts中绘图有2种方式。
普通方式绘图即先用类实例化,然后继续用对象中的各种方法添加属性
链式调用就是在实例化时的时候同时用各种方法添加属性
#柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
# 示例数据
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
data1 = [123, 153, 89, 107, 98, 23]
data2 = [56, 77, 93, 68, 45, 67]
bar=Bar()
bar.add_xaxis(cate)
bar.add_yaxis('电商渠道', data1)
bar.add_yaxis('门店', data2)
bar.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
bar.render_notebook()
#柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
# 示例数据
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
data1 = [123, 153, 89, 107, 98, 23]
data2 = [56, 77, 93, 68, 45, 67]
# 1.x版本支持链式调用
bar = (Bar()
.add_xaxis(cate)
.add_yaxis('电商渠道', data1)
.add_yaxis('门店', data2)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
)
bar.render_notebook()
像散点图、折线图等二维数据图形,它既有X轴,又有Y轴,所以我们要为X轴,Y轴分别添加数据。
.add_xaxis(xaxis_data=x)为X轴添加数据
.add_yaxis(series_name='', y_axis=y)为Y轴添加数据
像饼图、地图这样没有X轴、Y轴区分的图形,直接使用add()方法添加成对的数据即可
.add(series_name='', data_pair=[(i,j)for i,j in zip(lab,num)])
在添加数据时候,series_name参数是一个字符串,最终的作用是对应数据系列的名字,这个参数必须有,哪怕传递一个空字符串,否则会报错。
画完图需要用全局配置项,进行图形参数的调节与设置。全局配置项可通过调用set_global_options()方法进行设置,所有的全局配置项的使用,都是在options这个子模块下。
import pyecharts.options as opts
默认情况下图例配置项和提示框配置项是显示的,其他四个配置项默认情况下是不显示的,需要我们自己设置。
这部分涉及到的参数太多太杂,当你觉得某个图形需要怎么改的时候,一般肯定是有对应的参数进行处理的,这个时候你就需要去官网查!
.render(r"C:\Users\fff507\Desktop\test.html")
# 如果不指定路径,默认保存在当前工作环境目录下;
.render_notebook()
# 直接在notebook里显示;
make_snapshot 用于 pyecharts 直接生成图片,根据渲染引擎的不同,有selenium, phantomjs 和 pyppeteer 三种方式
def make_snapshot(
# 渲染引擎,可选 selenium 或者 phantomjs
engine: Any,
# 传入 HTML 文件路径
file_name: str,
# 输出图片路径
output_name: str,
# 延迟时间,避免图还没渲染完成就生成了图片,造成图片不完整
delay: float = 2,
# 像素比例,用于调节图片质量
pixel_ratio: int = 2,
# 渲染完图片是否删除原 HTML 文件
is_remove_html: bool = False,
# 浏览器类型,目前仅支持 Chrome, Safari,使用 snapshot-selenium 时有效
browser: str = "Chrome",
**kwargs,
)
下面以selenium为例(需要下载 browser driver)
#柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
#去掉警告信息
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
#渲染图片
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
# 数据
cate = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
data1 = [600, 620, 615, 605, 630, 510,523]
data2 = [510, 515, 505, 521, 530, 401,413]
# 1.x版本支持链式调用
bar = (Bar()
.add_xaxis(cate)
.add_yaxis('助力车', data1)
.add_yaxis('单车', data2)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-助力车单车骑行量对比", subtitle="副标题"))
)
fig=bar.render()
make_snapshot(snapshot,fig, "bar0.png")
更多详见渲染图片
#柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
#去掉警告信息
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
# 数据
cate = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
data1 = [600, 620, 615, 605, 630, 510,523]
data2 = [510, 515, 505, 521, 530, 401,413]
# 1.x版本支持链式调用
bar = (Bar()
.add_xaxis(cate)
.add_yaxis('助力车', data1)
.add_yaxis('单车', data2)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-助力车单车骑行量对比", subtitle="副标题"))
)
bar.render_notebook()
"""
系列配置项使用示例:
1. 不显示数值
2. 标记每个系列的最大值,最小值
3. 标记平均值
"""
bar = (Bar()
.add_xaxis(cate)
.add_yaxis('助力车', data1)
.add_yaxis('单车', data2)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]),
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average",name='平均值')]))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-助力车单车骑行量对比", subtitle="副标题"))
)
bar.render_notebook()
#饼图
from pyecharts.charts import Pie
from pyecharts import options as opts
#去掉警告信息
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
# 数据
cate = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
data1 = [600, 620, 615, 605, 630, 510,523]
pie = (Pie()
.add('骑行量', [list(z) for z in zip(cate, data)],
radius=["30%", "75%"],
rosetype="radius")
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-助力车", subtitle="副标题"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
)
pie.render_notebook()
#折线图
from pyecharts.charts import Line
from pyecharts import options as opts
#去掉警告信息
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
# 数据
cate = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
data1 = [600, 620, 615, 605, 630, 510,523]
data2 = [510, 515, 505, 521, 530, 401,413]
"""
折线图示例:
1. is_smooth 折线 OR 平滑
2. markline_opts 标记线 OR 标记点
"""
line = (Line()
.add_xaxis(cate)
.add_yaxis('助力车', data1,
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average",name='平均值')]))
.add_yaxis('单车', data2,
is_smooth=True,
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name="标记点",
coord=[cate[2], data2[2]], value=data2[2])]),
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average",name='平均值')]))
.set_global_opts(title_opts=opts.TitleOpts(title="Line-助力车vs单车", subtitle="副标题"))
)
line.render_notebook()
#热力图
from pyecharts.charts import HeatMap
from pyecharts import options as opts
from pyecharts.faker import Faker
import random
#去掉警告信息
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
# 数据
data = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)]
heat = (HeatMap()
.add_xaxis(Faker.clock)
.add_yaxis("访客数",
Faker.week,
data,
label_opts=opts.LabelOpts(is_show=True, position="inside"))
.set_global_opts(
title_opts=opts.TitleOpts(title="HeatMap-基本示例", subtitle="我是副标题"),
visualmap_opts=opts.VisualMapOpts(),
legend_opts=opts.LegendOpts(is_show=False))
)
heat.render_notebook()
#漏斗图
from pyecharts.charts import Funnel
from pyecharts import options as opts
#去掉警告信息
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
# 数据
cate = ['访问', '注册', '加入购物车', '提交订单', '付款成功']
data = [30398, 15230, 10045, 8109, 5698]
"""
漏斗图示例:
1. sort_控制排序,默认降序;
2. 标签显示位置
"""
funnel = (Funnel()
.add("用户数", [list(z) for z in zip(cate, data)],
sort_='ascending',
label_opts=opts.LabelOpts(position="inside"))
.set_global_opts(title_opts=opts.TitleOpts(title="Funnel-基本示例", subtitle="我是副标题"))
)
funnel.render_notebook()
#地理热力图
from pyecharts import options as opts
from pyecharts.charts import Map
import random
#去掉警告信息
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
province = ['广东', '湖北', '湖南', '四川', '重庆', '黑龙江', '浙江', '山西', '河北', '安徽', '河南', '山东', '西藏']
data = [(i, random.randint(50, 150)) for i in province]
_map = (
Map()
.add("销售额", data, "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-基本示例"),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),
)
)
_map.render_notebook()
#组合图
from pyecharts import options as opts
from pyecharts.charts import Map, Bar, Grid
from pyecharts.globals import ChartType, ThemeType
import random
cate = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
data1 = [600, 620, 615, 605, 630, 510,523]
bar = (Bar()
.add_xaxis(cate)
.add_yaxis('骑行量', data1)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]))
.set_global_opts(
title_opts=opts.TitleOpts(title="Grid-Bar")
)
)
line = (Line()
.add_xaxis(cate)
.add_yaxis('骑行量', data1,
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))
.set_global_opts(title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"))
)
grid = (
Grid()
.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
.add(line, grid_opts=opts.GridOpts(pos_top="60%"))
)
grid.render_notebook()
#时间轴
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
from pyecharts.globals import ThemeType
# 示例数据
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
tl = Timeline()
for i in range(2015, 2020):
bar = (
Bar()
.add_xaxis(cate)
.add_yaxis("线上", [random.randint(50, 150) for _ in cate])
.add_yaxis("门店", [random.randint(100, 200) for _ in cate])
.set_global_opts(title_opts=opts.TitleOpts("手机品牌{}年营业额".format(i)))
)
tl.add(bar, "{}年".format(i))
tl.render_notebook()
pyecharts 内置提供了 10+ 种不同的风格
默认设置是ThemeType.WHITE风格
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
Bar()
# 等价于 下面
Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
还有
ThemeType.LIGHT
ThemeType.DARK
ThemeType.CHALK
ThemeType.ESSOS
ThemeType.INFOGRAPHIC
ThemeType.MACARONS
ThemeType.ROMANTIC 等风格,还可以自己定义风格,更多见官网
以下是ThemeType.ROMANTIC风格示例
#主题设置
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
#去掉警告信息
import pyecharts
pyecharts.globals._WarningControl.ShowWarning = False
# 数据
cate = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
data1 = [600, 620, 615, 605, 630, 510,523]
data2 = [510, 515, 505, 521, 530, 401,413]
"""
主题设置:
默认white
"""
bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.ROMANTIC))
.add_xaxis(cate)
.add_yaxis('助力车', data1)
.add_yaxis('单车', data2)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),]))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-助力车单车骑行量对比", subtitle="Theme-ROMANTIC"))
)
bar.render_notebook()