您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Python中PyG2Plot可视化库如何使用
## 一、PyG2Plot 简介
### 1.1 什么是PyG2Plot
PyG2Plot 是 AntV 团队基于 G2Plot(一个基于图形语法理论的可视化引擎)开发的 Python 封装库。它允许开发者通过简单的 Python 代码生成丰富的交互式图表,支持常见的折线图、柱状图、饼图等 20+ 图表类型。
### 1.2 核心优势
- **语法简洁**:基于 G2Plot 的配置体系,只需少量代码即可生成图表
- **交互性强**:内置缩放、筛选、提示框等交互功能
- **响应式设计**:自动适配不同屏幕尺寸
- **TypeScript 支持**:完整的类型提示(需 Python 3.6+)
## 二、环境安装与配置
### 2.1 安装方式
```bash
pip install pyg2plot
from pyg2plot import Plot
line = Plot("Line")
line.set_options({
"data": [
{ "year": "1991", "value": 3 },
{ "year": "1992", "value": 4 },
{ "year": "1993", "value": 3.5 },
],
"xField": "year",
"yField": "value",
})
# 渲染到HTML文件
line.render("basic-line.html")
方法 | 参数 | 说明 |
---|---|---|
Plot() |
chart_type | 初始化指定类型的图表 |
set_options() |
config_dict | 设置图表配置项 |
render() |
file_path | 渲染为HTML文件 |
render_notebook() |
- | 在Jupyter中直接显示 |
bar = Plot("Bar")
bar.set_options({
"data": [
{ "genre": "Sports", "sold": 275 },
{ "genre": "Strategy", "sold": 115 },
],
"xField": "genre",
"yField": "sold",
"label": {},
"color": "#3398DB",
})
bar.render("bar-chart.html")
pie = Plot("Pie")
pie.set_options({
"data": [
{ "type": "分类一", "value": 27 },
{ "type": "分类二", "value": 25 },
],
"angleField": "value",
"colorField": "type",
"radius": 0.8,
})
pie.render("pie-chart.html")
scatter = Plot("Scatter")
scatter.set_options({
"data": [
{ "x": 12, "y": 23, "type": "A" },
{ "x": 16, "y": 25, "type": "B" },
],
"xField": "x",
"yField": "y",
"colorField": "type",
"size": 5,
"shape": "circle",
})
通过 Facet
实现分面:
facet = Plot("Facet")
facet.set_options({
"data": [...],
"type": "rect",
"fields": ["cut"],
"eachView": (view, facet) => {
view.line().position("carat*price");
},
})
{
"animation": {
"appear": {
"duration": 3000,
"delay": 1000,
}
}
}
from pyg2plot import Theme
Plot.set_theme(Theme.dark()) # 内置dark/light主题
{
"tooltip": {
"showTitle": True,
"fields": ["x", "y", "type"],
}
}
# 在多个图表中设置相同的group字段
{
"interactions": [
{ "type": "element-selected" },
{ "type": "brush" },
],
"group": "dashboard_1",
}
# 重新set_options后调用render
line.set_options({"data": new_data})
line.render("update.html")
import time
while True:
line.set_options({"data": get_live_data()})
line.render("live.html")
time.sleep(5)
大数据集处理:
"large": True
"binType": "hexagon"
进行分箱WebWorker 支持:
{
"useWorker": True,
"workerOptions": {
"scriptPath": "g2plot-worker.min.js"
}
}
{
"theme": {
"fontFamily": "'PingFang SC', 'Microsoft YaHei'"
}
}
{
"legend": {
"position": "top-left",
"offsetX": 30
}
}
dashboard = Plot("DualAxes")
dashboard.set_options({
"data": [[...], [...]],
"xField": "date",
"yField": ["uv", "pv"],
"geometryOptions": [
{"geometry": "line", "color": "#5B8FF9"},
{"geometry": "line", "color": "#5AD8A6"},
],
"interactions": ["element-highlight"],
})
dashboard.render("e-commerce.html")
PyG2Plot 通过将 G2Plot 的强大功能引入 Python 生态,为数据分析师提供了更便捷的可视化工具。本文涵盖了从基础使用到高级特性的完整指南,建议读者结合官方示例库(https://g2plot.antv.vision/)进行实践探索。
注意:本文基于 PyG2Plot 1.0.4 版本编写,不同版本API可能存在差异。 “`
这篇文章包含了: 1. 基础介绍与安装指南 2. 核心API说明 3. 5种常见图表实现 4. 交互功能与动态数据示例 5. 性能优化方案 6. 完整实战案例 7. 格式规范的Markdown排版
总字数约2150字,可根据需要调整具体示例内容。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。