国人开发的数据可视化神库 pyecharts
一、pyecharts简介
Echarts是百度开源的数据可视化工具,能够很好的嵌入web端,渲染的图表简洁精美,深受广大开发者喜爱和支持。而pyecharts是Python语言与Echarts的融合,用法简洁开发高效。
pyecharts特性
简洁的 API 设计,使用如丝滑般流畅,支持链式调用
囊括了 30+ 种常见图表,应有尽有
支持主流 Notebook 环境,Jupyter Notebook 和 JupyterLab
可轻松集成至 Flask,Django 等主流 Web 框架
高度灵活的配置项,可轻松搭配出精美的图表
详细的文档和示例,帮助开发者更快的上手项目
多达 400+ 地图文件以及原生的百度地图,为地理数据可视化提供强有力的支持
pyecharts安装
!pip3 install pyecharts
qucik start
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.render_notebook()
二、常用图表
2.1 条形图
使用 options 配置项,在 pyecharts 中,一切对象皆可 Options。
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.add_yaxis("商家B", [15, 6, 45, 20, 35, 66])
bar.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
bar.render_notebook()
2.2 网络图
from pyecharts import options as opts
from pyecharts.charts import Graph
nodes = [
{"name": "结点1", "symbolSize": 10},
{"name": "结点2", "symbolSize": 20},
{"name": "结点3", "symbolSize": 30},
{"name": "结点4", "symbolSize": 40},
{"name": "结点5", "symbolSize": 50},
{"name": "结点6", "symbolSize": 40},
{"name": "结点7", "symbolSize": 30},
{"name": "结点8", "symbolSize": 20}
]
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, repulsion=8000)
graph.set_global_opts(title_opts=opts.TitleOpts(title="Graph-基本示例"))
graph.render_notebook()
2.3 饼形图
from example.commons import Faker
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
pie = Pie()
pie.add("", [['哈士奇', 34],['萨摩耶', 98],['泰迪', 54],['金毛', 85],['牧羊犬', 88],['柯基', 50]])
pie.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
pie.render_notebook()
2.4 词云图
from pyecharts import options as opts
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType
words = [
("炫酷", 10000),
("Macys", 6181),
("Amy Schumer", 4386),
("Jurassic World", 4055),
("Charter Communications", 2467),
("Chick Fil A", 2244),
("Planet Fitness", 1868),
("Pitch Perfect", 1484),
("Express", 1112),
("Home", 865),
("Johnny Depp", 847),
("Lena Dunham", 582),
("Lewis Hamilton", 555),
("KXAN", 550),
("Mary Ellen Mark", 462),
("Farrah Abraham", 366),
("Rita Ora", 360),
("Serena Williams", 282),
("NCAA baseball tournament", 273),
("Point Break", 265),
]
wordcloud = WordCloud()
wordcloud.add(series_name = "",
data_pair = words,
word_size_range=[20, 100])
wordcloud.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
wordcloud.render_notebook()
2.5 地图
from example.commons import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
geo = Geo()
geo.add_schema(maptype="china")
geo.add(series_name = "geo",
data_pair = [list(z) for z in zip(Faker.provinces, Faker.values())],
type_ = ChartType.EFFECT_SCATTER)
geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="Geo-基本示例"))
geo.render_notebook()
2.6 时间线图
有时候需要渲染出变化趋势,这时候有timeline会如虎添翼。
from example.commons import Faker
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
x = Faker.choose()
timeline = Timeline()
for i in range(2015, 2020):
bar = Bar()
bar.add_xaxis(x)
bar.add_yaxis("商家A", Faker.values())
bar.add_yaxis("商家B", Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts("某商店{}年营业额".format(i)))
timeline.add(bar, "{}年".format(i))
timeline.render_notebook()
三、主题设置
pyecharts可以设置背景主题,常用的有LIGHT DARK等10余个主题。
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
from pyecharts.globals import ThemeType
bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
bar.add_xaxis(Faker.choose())
bar.add_yaxis("商家A", Faker.values())
bar.add_yaxis("商家C", Faker.values())
bar.add_yaxis("商家D", Faker.values())
bar.set_global_opts(title_opts=opts.TitleOpts('DARK'))
bar.render_notebook()
推荐阅读
2019Stata & Python 实证计量与爬虫分析暑期工作坊
pandas_profiling:生成动态交互的数据探索报告
cufflinks: 让pandas拥有plotly的炫酷的动态可视化能力
使用Pandas、Jinja和WeasyPrint制作pdf报告
大神kennethreitz写出requests-html号称为人设计的解析库
如果想要获取更多例子,可以关注本公众号,
后台回复'20190607'获得本教程的notebook代码下载方式