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 进行可视化并预览出来,十分方便!
开发原理
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 的配置文档,支持所有的图表、功能、特性,概念和结构上不作任何修改。
最后
欢迎给个 Star "_blank" href="https://github.com/antvis/G2Plot" rel="external nofollow" >AntV G2Plot
最后有任何的特性支持和 bug 反馈,欢迎 issue 或者 PR。
以上就是python 可视化库PyG2Plot的使用的详细内容,更多关于python 可视化库PyG2Plot的资料请关注其它相关文章!
更新日志
- 纯音入心系列纯音乐《古筝系列-弦凝指咽声停处》1CD[MP3][656.5MB]
- 群星.1994-大烂片2辑【派森】【WAV+CUE】
- 吴倩莲.1997-望爱【EMI百代】【WAV+CUE】
- 杨千嬅.2002-万紫千红演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郭采洁.2015-Begin.Again爱造飞鸡【华纳】【FLAC分轨】
- 许志安.2011-ON.AND.ON【东亚】【WAV+CUE】
- 潘秀琼.1994-木兰从军【名将】【WAV+CUE】
- 纯音入心系列纯音乐《古筝系列-当流行乐遇到古筝》1CD[MP3][1.9GB]
- 纯音入心系列纯音乐《精选古筝名曲100首》1CD[MP3][388.7MB]
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】