安装

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 重新保存,这样用浏览器打开的话就不会出现中文乱码问题了。

基本上所有的图表类型都是这样绘制的:

  1. chart_name = Type() 初始化具体类型图表。
  2. add() 添加数据及配置项。
  3. 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()
map-2

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使用的更多相关文章

  1. Python爬取南京市往年天气预报,使用pyecharts进行分析

    上一次分享了使用matplotlib对爬取的豆瓣书籍排行榜进行分析,但是发现python本身自带的这个绘图分析库还是有一些局限,绘图不够美观等,在网上搜索了一波,发现现在有很多的支持python的绘图 ...

  2. 数据分析——pyecharts

    导入类库 from pyecharts import Pie, Bar, Gauge, EffectScatter, WordCloud, Map, Grid, Line, Timeline impo ...

  3. Python:数据可视化pyecharts的使用

    什么是pyecharts? pyecharts 是一个用于生成 Echarts 图表的类库. echarts 是百度开源的一个数据可视化 JS 库,主要用于数据可视化.pyecharts 是一个用于生 ...

  4. Python中的可视化神器:pyecharts

    pyecharts是一款将python与echarts结合的强大的数据可视化工具,本文将为你阐述pyecharts的使用细则 前言 我们都知道python上的一款可视化工具matplotlib,而前些 ...

  5. 数据可视化之pyecharts

    Echarts 是百度开源的一个数据可视化 JS 库,主要用于数据可视化.pyecharts 是一个用于生成 Echarts 图表的类库.实际上就是 Echarts 与 Python 的对接. 安装 ...

  6. python可视化pyecharts

    python可视化pyecharts 简单介绍 pyecharts 是一个用于生成 Echarts 图表的类库.Echarts 是百度开源的一个数据可视化 JS 库.用 Echarts 生成的图可视化 ...

  7. 【转】Anaconda下安装pyecharts步骤及常见错误

    本文转载自:https://blog.csdn.net/skj1995/article/details/81187954 (1)之前看了几篇博客,有人说用cmd命令在目录C:\Users\Admini ...

  8. 发现一个强大的可视化第三方库pyecharts

    pyecharts 目前尚在不断的更新中,值得重点研究和学习的图表库

  9. 利用pyecharts做地图数据展示

    首先, pip install pyecharts 为了地图上的数据能显示完全,加载好需要的城市地理坐标数据. pip install echarts-countries-pypkg pip inst ...

随机推荐

  1. js判断参数是否为非数字

    isNaN() 函数用于检查其参数是否是非数字值.

  2. Linux知识要点大全(第二章)

    第二章   linux操作系统安装与配置主要内容 1:vmware虚拟机安装与使用 2:Linux系统安装前准备 3:Linux Centos 系统的安装 4:Centos 6.8的登录和关闭 5:C ...

  3. 机器学习之支持向量机—SVM原理代码实现

    支持向量机—SVM原理代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/9596898.html 1. 解决 ...

  4. 10分钟明白对偶建模法 / +Leampms的“主模型建模”和“对偶模型建模” 之 —— 三类最短路径问题

    摘要 对偶模型建模是非常有独特的一种建模方式 —— 当问题本身要求指标极小的情况下,对偶模型表现为求极大.本文给出三种最短路径问题的线性规划/混合整数规划模型,其中的第三类最短路径问题采用对偶建模方法 ...

  5. 内核mailbox

    只罗列增加取走消息: static int add_to_rbuf(struct mbox_chan *chan, void *mssg) { int idx; unsigned long flags ...

  6. 贝塞尔曲线控件 for .NET (EN)

    Conmajia 2012 Updated on Feb. 18, 2018 In Photoshop, there is a very powerful feature Curve Adjust, ...

  7. nginx优化之配置文件优化一常用参数

    #定义nginx运行的用户和用户组 user www www;   #启动进程,通常设置成和cpu的数量相等 worker_processes 8 ;   #为每个进程分配CPU,上面京8个进程分配到 ...

  8. pm2通过配置文件部署nodejs代码到服务器

    一.pm2文档地址: http://pm2.keymetrics.io/ 二.在本地项目下新建ecosystem.config.js文件,并且推送到远端(记得node_modules这个文件夹也得推送 ...

  9. 粮草先行——Android折叠屏开发技术点(一)

    最近有关折叠屏产品的新闻层出不穷,各家手机厂商也分别慢慢地亮出了自家的产品.然而市场上的一些APP仍然没有很好地适配这样的设备,显示不正常和应用重启的状况时有发生.因此,我会用接下来的几篇文章来点出有 ...

  10. C#工具:分词辅助类

    using System; using System.Collections; using System.IO; using System.Text.RegularExpressions; names ...