Python 可视化库:PyG2Plot

admin 2021年4月19日11:36:50评论117 views字数 2019阅读6分43秒阅读模式

来自公众号:Python那些事

G2 是蚂蚁金服开源一个基于图形语法,面向数据分析的统计图表引擎。G2Plot 是在 G2 基础上,屏蔽复杂概念的前提下,保留 G2 强大图形能力,封装出业务上常用的统计图表库。

G2Plot 是一个基于配置、体验优雅、面向数据分析的统计图表库,帮助开发者以最小成本绘制高质量统计图表。


那么对于很多 Python 语言环境的同学,如何使用 G2Plot 在进行数据分析之后的可视化呢?也就是


如何将 G2Plot 和 Python 结合起来?这里给出的就是基于 G2Plot 封装出 PyG2Plot,欢迎帮我充一下 Star。

如何使用

pip install pyg2plot

主要有 2 种使用方式(能力扩充中,欢迎提 issue)

1. 渲染出完整的 HTML

from pyg2plot import Plot

line = Plot("Line")

line.set_options({
"data": [
{ "year": "1991", "value": 3 },
{ "year": "1992", "value": 4 },
{ "year": "1993", "value": 3.5 },
{ "year": "1994", "value": 5 },
{ "year": "1995", "value": 4.9 },
{ "year": "1996", "value": 6 },
{ "year": "1997", "value": 7 },
{ "year": "1998", "value": 9 },
{ "year": "1999", "value": 13 },
],
"xField": "year",
"yField": "value",
})

# 1. render html file named plot.html
line.render("plot.html")

# 2. render html string
line.render_html()

这种情况可以用于:

  • 服务端 html 直出的场景

  • 生成可交互可视化分享

  • Excel 等工具嵌入的场景

2. 在 Jupyter notebook 中预览

from pyg2plot import Plot

line = Plot("Line")

line.set_options({
"height": 400, # set a default height in jupyter preview
"data": [
{ "year": "1991", "value": 3 },
{ "year": "1992", "value": 4 },
{ "year": "1993", "value": 3.5 },
{ "year": "1994", "value": 5 },
{ "year": "1995", "value": 4.9 },
{ "year": "1996", "value": 6 },
{ "year": "1997", "value": 7 },
{ "year": "1998", "value": 9 },
{ "year": "1999", "value": 13 },
],
"xField": "year",
"yField": "value",
})

line.render_notebook()

在我们做数据分析教程的过程中,可以将我们的数据使用 PyG2Plot 进行可视化并预览出来,十分方便!


Python 可视化库:PyG2Plot

开发原理

PyG2Plot 原理其实非常简单,其中借鉴了 pyecharts 的实现,但是因为蚂蚁金服的 G2Plot 完全基于可视分析理论的配置式结构,所以封装上比 pyecharts 简洁非常非常多。

基本的原理,就是通过 Python 语法提供 API,然后再调用 render 的时候,生成最终的 G2Plot HTML 文本,而针对不同的环境,生成的 HTML 稍有区别。

所以核心文件是:

  • plot.py:提供了 PyG2Plot 的几乎全部 API

  • engine.py:提供了渲染 HTML 的能力,其实是基于 jinjia2 这个模板引擎实现的,基本内容很少

  • templates:提供了所有的 jinjia2 模板文件,对于模板怎么用,jinjia2 的文档是非常非常详细的

使用文档

PyG2Plot 提供的 API 非常简单,使用上:

# 1. import
from pyg2plot import Plot

# 2. use a plot
line = Plot("Line")

# 3. set_options use G2Plot
line.set_options({ data, ... })

# 4. render
line.render_notebook()

而这其中 set_options API 的参数,是完全沿用 G2Plot 的配置文档,支持所有的图表、功能、特性,概念和结构上不作任何修改。

Python 可视化库:PyG2Plot

最后

欢迎给个 Star ‍♂️  有任何的特性支持和 bug 反馈,欢迎 issue 或者 PR。

https://github.com/hustcc/PyG2Plot

https://github.com/antvis/G2Plot

给上方的两个项目或者点击阅读原文给个Star吧!

(完)

推荐↓↓↓

Python 可视化库:PyG2Plot

人工智能与大数据技术

本文始发于微信公众号(Python编程):卧槽,一个牛逼的Python 可视化库:PyG2Plot

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年4月19日11:36:50
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Python 可视化库:PyG2Plothttp://cn-sec.com/archives/328283.html

发表评论

匿名网友 填写信息