关键词搜索

源码搜索 ×
×

一个牛逼的Python 可视化库:PyG2Plot

发布2021-02-25浏览372次

详情内容

G2 是蚂蚁金服开源一个基于图形语法,面向数据分析的统计图表引擎。G2Plot 是在 G2 基础上,屏蔽复杂概念的前提下,保留 G2 强大图形能力,封装出业务上常用的统计图表库。
G2Plot 是一个基于配置、体验优雅、面向数据python基础教程场景分析的统计图表库,帮助开发者以最小成本绘制高质量统计图表。

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

这里给出的就是基于 G2Plot 封装出 PyG2Plot

如何使用
pip install pyg2plot

  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 直出的c#教程生成可交互可视化分享
    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在我们做数据分析教程的过程中,可以将我们的数据使用 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 的配置文档,支持所有的图表、功能、特性,概念和结构上不作任何修改。

    最后
    G2Plot和PyG2Plot的地址
    https://github.com/hustcc/PyG2Plot

    https://github.com/antvis/G2Plot

    相关技术文章

    最新源码

    下载排行榜

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载