# 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。