01前言

 

Pyecharts在1.x版本之后迎来重大更新,与老版本(0.5X)已是两个完全不同的版本,所以很多小伙伴在使用Pyecharts出现了类似'pyecharts' has no attribute 'xxx'的报错,那是因为你安装了1.x的版本却使用了0.5x的调用方法。

 

当然如果你更习惯使用0.5X版本的可以通过如下语句来进行安装: pip install pyecharts==0.5.11

 

安装1.x版本(仅支持Python 3.6+): pip install pyecharts

 

本文将会介绍Pyecharts1.x版本的使用方法,本文所有语句均基于v1.6.2,通过以下语句查询使用pyecharts版本:

 

  •  
import pyecharts
print(pyecharts.__version__)

 

02基本使用

 

链式调用

 

pyecharts在v1.x之后支持链式调用,具体语句如下:

 

  •  
from pyecharts.charts import Barfrom 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()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python

 

注:其实这运行结果都是动态的,这里只放上截图,可在自己电脑上Jupyter 中运行查看!

 

全局配置

 

可以通过全局配置(.set_global_opts():)控制以下区域

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_02

 

系列配置

 

可以通过系列配置(.set_series_opts())控制图表中的文本,线样式,标记等。

 

  •  
"""系列配置项使用示例:1. 不显示数值2. 标记每个系列的最大值"""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="最大值"),]))       .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))      )
bar.render_notebook()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_03

 

03饼图

 

  •  
from pyecharts.charts import Piefrom pyecharts import options as opts
# 示例数据cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']data = [153, 124, 107, 99, 89, 46]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()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_04

 

04折线图

 

  •  
from pyecharts.charts import Linefrom 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. is_smooth 折线 OR 平滑2. markline_opts 标记线 OR 标记点"""line = (Line() .add_xaxis(cate) .add_yaxis('电商渠道', data1, markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")])) .add_yaxis('门店', data2, is_smooth=True, markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name="自定义标记点", coord=[cate[2], data2[2]], value=data2[2])])) .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例", subtitle="我是副标题")) )
line.render_notebook()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_05

 

05漏斗图

 

  •  
from pyecharts.charts import Funnelfrom pyecharts import options as opts
# 示例数据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()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_06

 

06热力图

 

  •  
from pyecharts.charts import HeatMapfrom pyecharts import options as optsfrom pyecharts.faker import Fakerimport random
# 示例数据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()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_07

 

07地理图

 

  •  
from pyecharts import options as optsfrom pyecharts.charts import Mapimport random
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()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_08

 

08地理热点图

 

  •  
from pyecharts import options as optsfrom pyecharts.charts import Geofrom pyecharts.globals import ChartTypeimport random
province = ['武汉', '十堰', '鄂州', '宜昌', '荆州', '孝感', '黄石', '咸宁', '仙桃']data = [(i, random.randint(50, 150)) for i in province]geo = (Geo(). add_schema(maptype="湖北") .add("门店数", data,type_=ChartType.HEATMAP) .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts( visualmap_opts=opts.VisualMapOpts(), legend_opts=opts.LegendOpts(is_show=False), title_opts=opts.TitleOpts(title="Geo-湖北热力地图")) )
geo.render_notebook()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_09

 

09组合图标

 

  •  
from pyecharts import options as optsfrom pyecharts.charts import Map, Bar, Gridfrom pyecharts.globals import ChartType, ThemeTypeimport random
province = ['武汉', '十堰', '鄂州', '宜昌', '荆州', '孝感', '黄石', '咸宁', '仙桃']data = [324, 125, 145, 216, 241, 244, 156, 278, 169]bar = (Bar() .add_xaxis(province) .add_yaxis('营业额', data) .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts( title_opts=opts.TitleOpts(title="Grid-Bar") ) )
line = (Line() .add_xaxis(province) .add_yaxis('营业额', data, 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()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_10

 

010主题设置

 

  •  
from pyecharts import options as optsfrom pyecharts.charts import Barfrom pyecharts.globals import ThemeType
# 示例数据cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']data1 = [123, 153, 89, 107, 98, 23]data2 = [56, 77, 93, 68, 45, 67]"""主题设置:默认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="Theme-ROMANTIC")) )
bar.render_notebook()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_11

 

011时间轴

 

  •  
from pyecharts import options as optsfrom pyecharts.charts import Bar, Timelinefrom 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()

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_12

 

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_13

超详细 pyecharts1.x 教程,让你的图表动起来~_Python_14