pyecharts使用
安装
pyecharts 兼容 Python2 和 Python3。目前版本为 0.1.2
pip install pyecharts
入门
首先开始来绘制你的第一个图表
from pyecharts import Bar bar = Bar("我的第一个图表", "这里是副标题")
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [, , , , , ])
bar.show_config()
bar.render()

Tip: 可以按右边的下载按钮将图片下载到本地
add()
主要方法,用于添加图表的数据和设置各种配置项show_config()
打印输出图表的所有配置项render()
默认将会在根目录下生成一个 render.html 的文件,支持 path 参数,设置文件保存位置,如 render(r"e:\my_first_chart.html"),文件用浏览器打开。
默认的编码类型为 UTF-8,在 Python3 中是没什么问题的,Python3 对中文的支持好很多。但是在 Python2 中,编码的处理是个很头疼的问题,暂时没能找到完美的解决方法,目前只能通过文本编辑器自己进行二次编码,我用的是 Visual Studio Code,先通过 Gbk 编码重新打开,然后再用 UTF-8 重新保存,这样用浏览器打开的话就不会出现中文乱码问题了。
基本上所有的图表类型都是这样绘制的:
chart_name = Type()
初始化具体类型图表。add()
添加数据及配置项。render()
生成 .html 文件。
Bar(柱状图/条形图)
from pyecharts import Bar bar = Bar("标记线和标记点示例")
bar.add("商家A", attr, v1, mark_point=["average"])
bar.add("商家B", attr, v2, mark_line=["min", "max"])
bar.render()

from pyecharts import Bar bar = Bar("x 轴和 y 轴交换")
bar.add("商家A", attr, v1)
bar.add("商家B", attr, v2, is_convert=True)
bar.render()

EffectScatter(带有涟漪特效动画的散点图)
from pyecharts import EffectScatter v1 = [, , , , , ]
v2 = [, , , , , ]
es = EffectScatter("动态散点图示例")
es.add("effectScatter", v1, v2)
es.render()

es = EffectScatter("动态散点图各种图形示例")
es.add("", [], [], symbol_size=, effect_scale=3.5, effect_period=, symbol="pin")
es.add("", [], [], symbol_size=, effect_scale=4.5, effect_period=,symbol="rect")
es.add("", [], [], symbol_size=, effect_scale=5.5, effect_period=,symbol="roundRect")
es.add("", [], [], symbol_size=, effect_scale=6.5, effect_brushtype='fill',symbol="diamond")
es.add("", [], [], symbol_size=, effect_scale=5.5, effect_period=,symbol="arrow")
es.add("", [], [], symbol_size=, effect_scale=2.5, effect_period=,symbol="triangle")
es.render()

Funnel(漏斗图)
from pyecharts import Funnel attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
value = [, , , , , ]
funnel = Funnel("漏斗图示例")
funnel.add("商品", attr, value, is_label_show=True, label_pos="inside", label_text_color="#fff")
funnel.render()

Gauge(仪表盘)
from pyecharts import Gauge gauge = Gauge("仪表盘示例")
gauge.add("业务指标", "完成率", 66.66)
gauge.show_config()
gauge.render()

Geo(地理坐标系)
from pyecharts import Geo data = [
("海门", ),("鄂尔多斯", ),("招远", ),("舟山", ),("齐齐哈尔", ),("盐城", ),
("赤峰", ),("青岛", ),("乳山", ),("金昌", ),("泉州", ),("莱西", ),
("日照", ),("胶南", ),("南通", ),("拉萨", ),("云浮", ),("梅州", )...]
geo = Geo("全国主要城市空气质量", "data from pm2.5", title_color="#fff", title_pos="center",
width=, height=, background_color='#404a59')
attr, value = geo.cast(data)
geo.add("", attr, value, visual_range=[, ], visual_text_color="#fff", symbol_size=, is_visualmap=True)
geo.show_config()
geo.render()

from pyecharts import Geo data = [("海门", ), ("鄂尔多斯", ), ("招远", ), ("舟山", ), ("齐齐哈尔", ), ("盐城", )]
geo = Geo("全国主要城市空气质量", "data from pm2.5", title_color="#fff", title_pos="center",
width=, height=, background_color='#404a59')
attr, value = geo.cast(data)
geo.add("", attr, value, type="effectScatter", is_random=True, effect_scale=)
geo.show_config()
geo.render()

Graph(关系图)
from pyecharts import Graph nodes = [{"name": "结点1", "symbolSize": },
{"name": "结点2", "symbolSize": },
{"name": "结点3", "symbolSize": },
{"name": "结点4", "symbolSize": },
{"name": "结点5", "symbolSize": },
{"name": "结点6", "symbolSize": },
{"name": "结点7", "symbolSize": },
{"name": "结点8", "symbolSize": }]
links = []
for i in nodes:
for j in nodes:
links.append({"source": i.get('name'), "target": j.get('name')})
graph = Graph("关系图-环形布局示例")
graph.add("", nodes, links, is_label_show=True, repulsion=, layout='circular', label_text_color=None)
graph.show_config()
graph.render()

from pyecharts import Graph import json
with open("..\json\weibo.json", "r", encoding="utf-8") as f:
j = json.load(f)
nodes, links, categories, cont, mid, userl = j
graph = Graph("微博转发关系图", width=, height=)
graph.add("", nodes, links, categories, label_pos="right", repulsion=, is_legend_show=False,
line_curve=0.2, label_text_color=None)
graph.show_config()
graph.render()
Line(折线/面积图)
from pyecharts import Line attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [, , , , , ]
v2 = [, , , , , ]
line = Line("折线图示例")
line.add("商家A", attr, v1, mark_point=["average"])
line.add("商家B", attr, v2, is_smooth=True, mark_line=["max", "average"])
line.show_config()
line.render()

line = Line("折线图-阶梯图示例")
line.add("商家A", attr, v1, is_step=True, is_label_show=True)
line.show_config()
line.render()

line = Line("折线图-面积图示例")
line.add("商家A", attr, v1, is_fill=True, line_opacity=0.2, area_opacity=0.4, symbol=None)
line.add("商家B", attr, v2, is_fill=True, area_color='#000', area_opacity=0.3, is_smooth=True)
line.show_config()
line.render()

Liquid(水球图)
from pyecharts import Liquid liquid = Liquid("水球图示例")
liquid.add("Liquid", [0.6])
liquid.show_config()
liquid.render()

from pyecharts import Liquid liquid = Liquid("水球图示例")
liquid.add("Liquid", [0.6, 0.5, 0.4, 0.3], is_liquid_animation=False, shape='diamond')
liquid.show_config()
liquid.render()

Map(地图)

from pyecharts import Map value = [, , , , ]
attr = ['汕头市', '汕尾市', '揭阳市', '阳江市', '肇庆市']
map = Map("广东地图示例", width=, height=)
map.add("", attr, value, maptype='广东', is_visualmap=True, visual_text_color='#000')
map.show_config()
map.render()

Parallel(平行坐标系)
from pyecharts import Parallel c_schema = [
{"dim": , "name": "data"},
{"dim": , "name": "AQI"},
{"dim": , "name": "PM2.5"},
{"dim": , "name": "PM10"},
{"dim": , "name": "CO"},
{"dim": , "name": "NO2"},
{"dim": , "name": "CO2"},
{"dim": , "name": "等级",
"type": "category", "data": ['优', '良', '轻度污染', '中度污染', '重度污染', '严重污染']}
]
data = [
[, , , , 0.82, , , "良"],
[, , , , 0.86, , , "良"],
[, , , , 1.09, , , "良"],
[, , , , 1.28, , , "轻度污染"],
[, , , , 1.07, , , "轻度污染"],
[, , , , 1.28, , , "轻度污染"],
[, , , , 1.07, , , "轻度污染"],
[, , , , 0.86, , , "良"],
[, , , , 0.64, , , "良"],
[, , , , 1.01, , , "良"],
[, , , , 1.03, , , "轻度污染"],
[, , , , 1.1, , , "良"],
[, , , , 1.28, , , "良"],
[, , , , 1.47, , , "轻度污染"]
]
parallel = Parallel("平行坐标系-用户自定义指示器")
parallel.config(c_schema=c_schema)
parallel.add("parallel", data)
parallel.show_config()
parallel.render()

Pie(饼图)
from pyecharts import Pie attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [, , , , , ]
pie = Pie("饼图示例")
pie.add("", attr, v1, is_label_show=True)
pie.show_config()
pie.render()

from pyecharts import Pie attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [, , , , , ]
v2 = [, , , , , ]
pie = Pie("饼图-玫瑰图示例", title_pos='center', width=)
pie.add("商品A", attr, v1, center=[, ], is_random=True, radius=[, ], rosetype='radius')
pie.add("商品B", attr, v2, center=[, ], is_random=True, radius=[, ], rosetype='area',
is_legend_show=False, is_label_show=True)
pie.show_config()
pie.render()
Polar(极坐标系)
from pyecharts import Polar radius = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
polar = Polar("极坐标系-堆叠柱状图示例", width=, height=)
polar.add("A", [, , , , , , ], radius_data=radius, type='barRadius', is_stack=True)
polar.add("B", [, , , , , , ], radius_data=radius, type='barRadius', is_stack=True)
polar.add("C", [, , , , , , ], radius_data=radius, type='barRadius', is_stack=True)
polar.show_config()
polar.render()

from pyecharts import Polar radius = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
polar = Polar("极坐标系-堆叠柱状图示例", width=, height=)
polar.add("", [, , , , , , ], radius_data=radius, type='barAngle', is_stack=True)
polar.add("", [, , , , , , ], radius_data=radius, type='barAngle', is_stack=True)
polar.add("", [, , , , , , ], radius_data=radius, type='barAngle', is_stack=True)
polar.show_config()
polar.render()

Radar(雷达图)
from pyecharts import Radar schema = [
("销售", ), ("管理", ), ("信息技术", ), ("客服", ), ("研发", ), ("市场", )]
v1 = [[, , , , , ]]
v2 = [[, , , , , ]]
radar = Radar()
radar.config(schema)
radar.add("预算分配", v1, is_splitline=True, is_axisline_show=True)
radar.add("实际开销", v2, label_color=["#4e79a7"], is_area_show=False)
radar.show_config()
radar.render()

value_bj = [
[, , , 0.46, , , ], [, , , 0.65, , , ],
[, , , 0.3, , , ], [, , , 0.33, , , ]...]
value_sh = [
[, , , 0.82, , , ], [, , , 0.86, , , ],
[, , , 1.09, , , ], [, , , 1.28, , , ]...]
c_schema= [{"name": "AQI", "max": , "min": },
{"name": "PM2.5", "max": , "min": },
{"name": "PM10", "max": , "min": },
{"name": "CO", "max": },
{"name": "NO2", "max": },
{"name": "SO2", "max": }]
radar = Radar()
radar.config(c_schema=c_schema, shape='circle')
radar.add("北京", value_bj, item_color="#f9713c", symbol=None)
radar.add("上海", value_sh, item_color="#b3e4a1", symbol=None)
radar.show_config()
radar.render()
Scatter(散点图)
from pyecharts import Scatter v1 = [, , , , , ]
v2 = [, , , , , ]
scatter = Scatter("散点图示例")
scatter.add("A", v1, v2)
scatter.add("B", v1[::-], v2)
scatter.show_config()
scatter.render()

from pyecharts import Scatter scatter = Scatter("散点图示例")
v1, v2 = scatter.draw("../images/pyecharts-0.png")
scatter.add("pyecharts", v1, v2, is_random=True)
scatter.show_config()
scatter.render()

WordCloud(词云图)
from pyecharts import WordCloud name = ['Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications',
'Chick Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp',
'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham',
'Rita Ora', 'Serena Williams', 'NCAA baseball tournament', 'Point Break']
value = [, , , , , , , , , , , , ,
, , , , , , ]
wordcloud = WordCloud(width=, height=)
wordcloud.add("", name, value, word_size_range=[, ])
wordcloud.show_config()
wordcloud.render()

用户自定义
用户还可以自定义结合 Line/Bar 图表
需使用 get_series()
和 custom()
方法
get_series()
""" 获取图表的 series 数据 """ custom(series)
''' 追加自定义图表类型 '''
- series -> dict
追加图表类型的 series 数据
先用 get_series()
获取数据,再使用 custom()
将图表结合在一起
from pyecharts import Bar, Line attr = ['A', 'B', 'C', 'D', 'E', 'F']
v1 = [, , , , , ]
v2 = [, , , , , ]
v3 = [, , , , , ]
bar = Bar("Line - Bar 示例")
bar.add("bar", attr, v1)
line = Line()
line.add("line", v2, v3)
bar.custom(line.get_series())
bar.show_config()
bar.render()

更多示例
用极坐标系画出一个爱心
import math
from pyecharts import Polar data = []
for i in range():
theta = i / *
r = * ( + math.sin(theta / * math.pi))
data.append([r, theta])
hour = [i for i in range(, )]
polar = Polar("极坐标系示例", width=, height=)
polar.add("Love", data, angle_data=hour, boundary_gap=False,start_angle=)
polar.show_config()
polar.render()

用极坐标系画出一朵小花
import math
from pyecharts import Polar data = []
for i in range():
t = i / * math.pi
r = math.sin( * t) * math.cos( * t)
data.append([r, i])
polar = Polar("极坐标系示例", width=, height=)
polar.add("Flower", data, start_angle=, symbol=None, axis_range=[, None])
polar.show_config()
polar.render()

还可以给小花涂上颜色
import math
from pyecharts import Polar data = []
for i in range():
t = i / * math.pi
r = math.sin( * t) * math.cos( * t)
data.append([r, i])
polar = Polar("极坐标系示例", width=, height=)
polar.add("Color-Flower", data, start_angle=, symbol=None, axis_range=[, None],
area_color="#f71f24", area_opacity=0.6)
polar.show_config()
polar.render()

用散点图画出一个爱心
from pyecharts import Scatter scatter = Scatter("散点图示例", width=, height=)
v1 ,v2 = scatter.draw("../images/love.png")
scatter.add("Love", v1, v2)
scatter.render()

用散点图画出一个火辣的 Bra
from pyecharts import Scatter scatter = Scatter("散点图示例", width=, height=)
v1 ,v2 = scatter.draw("../images/cup.png")
scatter.add("Cup", v1, v2)
scatter.render()

用散点图画出一个性感的 Bra
from pyecharts import Scatter scatter = Scatter("散点图示例", width=, height=)
v1 ,v2 = scatter.draw("../images/cup.png")
scatter.add("Cup", v1, v2, label_color=["#000"])
scatter.render()

某地最低温和最高气温折线图
from pyecharts import Line attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日', ]
line = Line("折线图示例")
line.add("最高气温", attr, [, , , , , , ], mark_point=["max", "min"], mark_line=["average"])
line.add("最低气温", attr, [, -, , , , , ], mark_point=["max", "min"],
mark_line=["average"], yaxis_formatter="°C")
line.show_config()
line.render()

饼图嵌套
from pyecharts import Pie pie = Pie("饼图示例", title_pos='center', width=, height=)
pie.add("", ['A', 'B', 'C', 'D', 'E', 'F'], [, , , , , ], radius=[, ],is_label_show=True)
pie.add("", ['H', 'I', 'J'], [, , ], radius=[, ], legend_orient='vertical', legend_pos='left')
pie.show_config()
pie.render()

饼图再嵌套
import random
from pyecharts import Pie attr = ['A', 'B', 'C', 'D', 'E', 'F']
pie = Pie("饼图示例", width=, height=)
pie.add("", attr, [random.randint(, ) for _ in range()], radius=[, ], center=[, ],is_random=True)
pie.add("", attr, [random.randint(, ) for _ in range()], radius=[, ], center=[, ],rosetype='area')
pie.add("", attr, [random.randint(, ) for _ in range()], radius=[, ], center=[, ],is_random=True)
pie.add("", attr, [random.randint(, ) for _ in range()], radius=[, ], center=[, ],rosetype='radius')
pie.show_config()
pie.render()

某地的降水量和蒸发量柱状图
from pyecharts import Bar attr = ["{}月".format(i) for i in range(, )]
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
bar = Bar("柱状图示例")
bar.add("蒸发量", attr, v1, mark_line=["average"], mark_point=["max", "min"])
bar.add("降水量", attr, v2, mark_line=["average"], mark_point=["max", "min"])
bar.show_config()
bar.render()

各类电影中"好片"所占的比例
from pyecharts import Pie pie = Pie('各类电影中"好片"所占的比例', "数据来着豆瓣", title_pos='center')
pie.add("", ["剧情", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None, )
pie.add("", ["奇幻", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None, legend_pos='left')
pie.add("", ["爱情", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["惊悚", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["冒险", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["动作", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["喜剧", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["科幻", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["悬疑", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["犯罪", ""], [, ], center=[, ], radius=[, ],
label_pos='center', is_label_show=True, label_text_color=None, is_legend_show=True, legend_top="center")
pie.show_config()
pie.render()

用极坐标系画出一个蜗牛壳
import math
from pyecharts import Polar data = []
for i in range():
for j in range():
theta = j / *
alpha = i * + theta
r = math.pow(math.e, 0.003 * alpha)
data.append([r, theta])
polar = Polar("极坐标系示例")
polar.add("", data, symbol_size=, symbol='circle', start_angle=-, is_radiusaxis_show=False,
area_color="#f3c5b3", area_opacity=0.5, is_angleaxis_show=False)
polar.show_config()
polar.render()

pyecharts使用的更多相关文章
- Python爬取南京市往年天气预报,使用pyecharts进行分析
上一次分享了使用matplotlib对爬取的豆瓣书籍排行榜进行分析,但是发现python本身自带的这个绘图分析库还是有一些局限,绘图不够美观等,在网上搜索了一波,发现现在有很多的支持python的绘图 ...
- 数据分析——pyecharts
导入类库 from pyecharts import Pie, Bar, Gauge, EffectScatter, WordCloud, Map, Grid, Line, Timeline impo ...
- Python:数据可视化pyecharts的使用
什么是pyecharts? pyecharts 是一个用于生成 Echarts 图表的类库. echarts 是百度开源的一个数据可视化 JS 库,主要用于数据可视化.pyecharts 是一个用于生 ...
- Python中的可视化神器:pyecharts
pyecharts是一款将python与echarts结合的强大的数据可视化工具,本文将为你阐述pyecharts的使用细则 前言 我们都知道python上的一款可视化工具matplotlib,而前些 ...
- 数据可视化之pyecharts
Echarts 是百度开源的一个数据可视化 JS 库,主要用于数据可视化.pyecharts 是一个用于生成 Echarts 图表的类库.实际上就是 Echarts 与 Python 的对接. 安装 ...
- python可视化pyecharts
python可视化pyecharts 简单介绍 pyecharts 是一个用于生成 Echarts 图表的类库.Echarts 是百度开源的一个数据可视化 JS 库.用 Echarts 生成的图可视化 ...
- 【转】Anaconda下安装pyecharts步骤及常见错误
本文转载自:https://blog.csdn.net/skj1995/article/details/81187954 (1)之前看了几篇博客,有人说用cmd命令在目录C:\Users\Admini ...
- 发现一个强大的可视化第三方库pyecharts
pyecharts 目前尚在不断的更新中,值得重点研究和学习的图表库
- 利用pyecharts做地图数据展示
首先, pip install pyecharts 为了地图上的数据能显示完全,加载好需要的城市地理坐标数据. pip install echarts-countries-pypkg pip inst ...
随机推荐
- 最小生成树之Kruskal(克鲁斯卡尔)算法
学习最小生成树算法之前我们先来了解下下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的所 ...
- Oracle AWR报告生成和性能分析
目录 一.AWE报告生成步骤 1.1 工具选择 1.2 自动创建快照 1.3 手工创建快照 1.4 生成AWR报告 二.AWR报告分析 2.1 AWR之DB Time 2.2 AWR之load_pro ...
- Hadoop系列006-HDFS概念及命令行操作
本人微信公众号,欢迎扫码关注! HDFS概念及命令行操作 一.HDFS概念 1.1 概念 HDFS,它是一个文件系统,用于存储文件,通过目录树来定位文件:其次,它是分布式的,由很多服务器联合起来实现其 ...
- 使用ASP.NET Core开发GraphQL服务器 -- 预备知识(下)
上一篇文章:https://www.cnblogs.com/cgzl/p/9734083.html 处理数据 嵌套字段 看例子: 我想查看viewer下的repositories.注意里面的edges ...
- 使用 Moq 测试.NET Core 应用 -- Mock 属性
第一篇文章, 关于Mock的概念介绍: https://www.cnblogs.com/cgzl/p/9294431.html 第二篇文章, 关于方法Mock的介绍: https://www.cnbl ...
- Redhat 平台下 LVM 管理说明
Redhat 平台下 LVM 管理说明 LVM 是 Logical Volume Manager(逻辑卷管理器)的简写,它为主机提供了更高层次的磁盘存储管理能力.LVM 可以帮助系统管理员为应用与用 ...
- docker安装Oracle 12c
1.安装阿里的docker源: cat /etc/docker/daemon.json { "registry-mirrors": ["https://pee6w651. ...
- 聊聊在AOP模式下的缓存方案
面向方法的数据集缓存 使用了autofac做为ioc容器,使用Autofac.Extras.DynamicProxy2作为方法拦截器,缓存面向方法,直接在方法上添加CachingAttribute特性 ...
- js 实现 复制 功能 (zeroclipboard)
#复制功能因访问权限和安全问题, 被浏览器禁了# 我要实现的功能:点击复制按钮,复制浏览器的当前页面地址,加上用户选择的参数(用户查找过滤),直接将该链接发给别人,点击打开就是对应的查找结果而不是默认 ...
- 【我们一起写框架】MVVM的WPF框架(四)—DataGrid
前言 这个框架写到这里,应该有很多同学发现,框架很多地方的细节,其实是违背了MVVM的设计逻辑的. 没错,它的确是违背了. 但为什么明知道违背设计逻辑,还要这样编写框架呢? 那是因为,我们编写的是框架 ...