关于python:????-Python-上的可视化库PyG2Plot

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 ????‍♂️

  • AntV G2Plot
  • PyG2Plot

最初有任何的个性反对和 bug 反馈,欢送 issue 或者 PR。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理