其它

国人开发的数据可视化神库 pyecharts

2021-01-20 07:11:17 阅读数 2118 收藏 0

一、pyecharts简介

Echarts是百度开源的数据可视化工具,能够很好的嵌入web端,渲染的图表简洁精美,深受广大开发者喜爱和支持。而pyecharts是Python语言与Echarts的融合,用法简洁开发高效。

pyecharts特性

  • 简洁的 API 设计,使用如丝滑般流畅,支持链式调用

  • 囊括了 30+ 种常见图表,应有尽有

  • 支持主流 Notebook 环境,Jupyter Notebook 和 JupyterLab

  • 可轻松集成至 Flask,Django 等主流 Web 框架

  • 高度灵活的配置项,可轻松搭配出精美的图表

  • 详细的文档和示例,帮助开发者更快的上手项目

  • 多达 400+ 地图文件以及原生的百度地图,为地理数据可视化提供强有力的支持

pyecharts安装

  
  
  1. !pip3 install pyecharts

qucik start

  
  
  1. from pyecharts.charts import Bar


  2. bar = Bar()

  3. bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])

  4. bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])

  5. bar.render_notebook()


二、常用图表

2.1 条形图

使用 options 配置项,在 pyecharts 中,一切对象皆可 Options。

  
  
  1. from pyecharts.charts import Bar

  2. from pyecharts import options as opts


  3. bar = Bar()

  4. bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])

  5. bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])

  6. bar.add_yaxis("商家B", [15, 6, 45, 20, 35, 66])

  7. bar.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))

  8. bar.render_notebook()

2.2 网络图

  
  
  1. from pyecharts import options as opts

  2. from pyecharts.charts import Graph



  3. nodes = [

  4. {"name": "结点1", "symbolSize": 10},

  5. {"name": "结点2", "symbolSize": 20},

  6. {"name": "结点3", "symbolSize": 30},

  7. {"name": "结点4", "symbolSize": 40},

  8. {"name": "结点5", "symbolSize": 50},

  9. {"name": "结点6", "symbolSize": 40},

  10. {"name": "结点7", "symbolSize": 30},

  11. {"name": "结点8", "symbolSize": 20}

  12. ]


  13. links = []

  14. for i in nodes:

  15. for j in nodes:

  16. links.append({"source": i.get("name"), "target": j.get("name")})


  17. graph = Graph()

  18. graph.add("", nodes, links, repulsion=8000)

  19. graph.set_global_opts(title_opts=opts.TitleOpts(title="Graph-基本示例"))


  20. graph.render_notebook()

2.3 饼形图

  
  
  1. from example.commons import Faker

  2. from pyecharts import options as opts

  3. from pyecharts.charts import Page, Pie


  4. pie = Pie()

  5. pie.add("", [['哈士奇', 34],['萨摩耶', 98],['泰迪', 54],['金毛', 85],['牧羊犬', 88],['柯基', 50]])

  6. pie.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))

  7. pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))


  8. pie.render_notebook()

2.4 词云图

  
  
  1. from pyecharts import options as opts

  2. from pyecharts.charts import WordCloud

  3. from pyecharts.globals import SymbolType



  4. words = [

  5. ("炫酷", 10000),

  6. ("Macys", 6181),

  7. ("Amy Schumer", 4386),

  8. ("Jurassic World", 4055),

  9. ("Charter Communications", 2467),

  10. ("Chick Fil A", 2244),

  11. ("Planet Fitness", 1868),

  12. ("Pitch Perfect", 1484),

  13. ("Express", 1112),

  14. ("Home", 865),

  15. ("Johnny Depp", 847),

  16. ("Lena Dunham", 582),

  17. ("Lewis Hamilton", 555),

  18. ("KXAN", 550),

  19. ("Mary Ellen Mark", 462),

  20. ("Farrah Abraham", 366),

  21. ("Rita Ora", 360),

  22. ("Serena Williams", 282),

  23. ("NCAA baseball tournament", 273),

  24. ("Point Break", 265),

  25. ]



  26. wordcloud = WordCloud()

  27. wordcloud.add(series_name = "",

  28. data_pair = words,

  29. word_size_range=[20, 100])

  30. wordcloud.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))


  31. wordcloud.render_notebook()

2.5 地图

  
  
  1. from example.commons import Faker

  2. from pyecharts import options as opts

  3. from pyecharts.charts import Geo

  4. from pyecharts.globals import ChartType, SymbolType



  5. geo = Geo()

  6. geo.add_schema(maptype="china")

  7. geo.add(series_name = "geo",

  8. data_pair = [list(z) for z in zip(Faker.provinces, Faker.values())],

  9. type_ = ChartType.EFFECT_SCATTER)


  10. geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False))

  11. geo.set_global_opts(

  12. visualmap_opts=opts.VisualMapOpts(),

  13. title_opts=opts.TitleOpts(title="Geo-基本示例"))


  14. geo.render_notebook()

2.6 时间线图

有时候需要渲染出变化趋势,这时候有timeline会如虎添翼。

  
  
  1. from example.commons import Faker

  2. from pyecharts import options as opts

  3. from pyecharts.charts import Bar, Timeline


  4. x = Faker.choose()

  5. timeline = Timeline()

  6. for i in range(2015, 2020):

  7. bar = Bar()

  8. bar.add_xaxis(x)

  9. bar.add_yaxis("商家A", Faker.values())

  10. bar.add_yaxis("商家B", Faker.values())

  11. bar.set_global_opts(title_opts=opts.TitleOpts("某商店{}年营业额".format(i)))

  12. timeline.add(bar, "{}年".format(i))


  13. timeline.render_notebook()

三、主题设置

pyecharts可以设置背景主题,常用的有LIGHT DARK等10余个主题。

  
  
  1. from pyecharts import options as opts

  2. from pyecharts.charts import Bar, Timeline

  3. from pyecharts.globals import ThemeType


  4. bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))

  5. bar.add_xaxis(Faker.choose())

  6. bar.add_yaxis("商家A", Faker.values())

  7. bar.add_yaxis("商家C", Faker.values())

  8. bar.add_yaxis("商家D", Faker.values())

  9. bar.set_global_opts(title_opts=opts.TitleOpts('DARK'))



  10. bar.render_notebook()


推荐阅读

【视频课】文本数据分析快速入门

2019Stata & Python 实证计量与爬虫分析暑期工作坊

pandas_profiling:生成动态交互的数据探索报告

cufflinks: 让pandas拥有plotly的炫酷的动态可视化能力

使用Pandas、Jinja和WeasyPrint制作pdf报告

使用Pandas更好的做数据科学

使用Pandas更好的做数据科学(二)

少有人知的python数据科学库

folium:地图数据可视化库

学习编程遇到问题,该如何正确的提问?

100G 文本分析语料资源(免费下载) 

如何用Google Colab高效的学习Python

大神kennethreitz写出requests-html号称为人设计的解析库

flashtext:大规模文本数据清洗利器

如果想要获取更多例子,可以关注本公众号,

后台回复'20190607'获得本教程的notebook代码下载方式