gpt4 book ai didi

python - ArangoDB pyArango 的图形绘图 API

转载 作者:行者123 更新时间:2023-11-28 18:13:59 25 4
gpt4 key购买 nike

我使用的是 ArangoDB 社区版,我可以在 AQL 中查询创建的图,并以 JSON 格式获取结果,该结果在 ArangoDB 网络界面工具上以图形方式可视化。

AQL查询

FOR v,e,p IN 1..3 OUTBOUND 'germanCity/Hamburg' GRAPH 'routeplanner' 
OPTIONS{bfs :true}
RETURN p

JSON输出

[
{
"edges": [
{
"_key": "6392826",
"_id": "germanHighway/6392826",
"_from": "germanCity/Hamburg",
"_to": "germanCity/Cologne",
"_rev": "_WmZ77pW--D",
"distance": 500
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Cologne",
"_id": "germanCity/Cologne",
"_rev": "_WmZ77Y6--B",
"population": 1000000,
"isCapital": false,
"loc": [
50.9364,
6.9528
]
}
]
},
{
"edges": [
{
"_key": "6392840",
"_id": "internationalHighway/6392840",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Paris",
"_rev": "_WmZ77pa--_",
"distance": 900
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Paris",
"_id": "frenchCity/Paris",
"_rev": "_WmZ77Z---D",
"population": 4000000,
"isCapital": true,
"loc": [
48.8567,
2.3508
]
}
]
},
{
"edges": [
{
"_key": "6392843",
"_id": "internationalHighway/6392843",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Lyon",
"_rev": "_WmZ77pa--B",
"distance": 1300
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Lyon",
"_id": "frenchCity/Lyon",
"_rev": "_WmZ77Z---B",
"population": 80000,
"isCapital": false,
"loc": [
45.76,
4.84
]
}
]
}
]

等效图

Graph Viewer

因为我们可以在 Web 界面中获得可视化图形输出,所以我想在 Language<->ArangoDB 中显示相同的内容。这里的语言可以是支持的驱动语言:Python、Java、C#等。

我正在使用 pyArango 来连接 ArangoDB

我找不到一个 ArangoDB API 来获取 JPG 或 matlibplot 中的图形可视化。

除了使用以下两个选项,还有其他方法吗?

  1. 使用networkx.draw(networkx.graph)
  2. matplotlib.pyplot

最佳答案

如果您需要图形可视化,那么 Graphviz图书馆适合你。如果 Python 没问题,那么您只需要一个 Python 绑定(bind)库 graphviz (在内部使用 DOT language 表示。)

将图形 JSON 从 Arango DB 提供给 graphviz 进行渲染非常容易。

您可以根据自己的风格对其进行自定义,添加标签、颜色、 reshape 节点等。

这是您的示例 JSON 的一个简单示例:

from graphviz import Digraph

arango_graph = [
{
"edges": [
{
"_key": "6392826",
"_id": "germanHighway/6392826",
"_from": "germanCity/Hamburg",
"_to": "germanCity/Cologne",
"_rev": "_WmZ77pW--D",
"distance": 500
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Cologne",
"_id": "germanCity/Cologne",
"_rev": "_WmZ77Y6--B",
"population": 1000000,
"isCapital": False,
"loc": [
50.9364,
6.9528
]
}
]
},
{
"edges": [
{
"_key": "6392840",
"_id": "internationalHighway/6392840",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Paris",
"_rev": "_WmZ77pa--_",
"distance": 900
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Paris",
"_id": "frenchCity/Paris",
"_rev": "_WmZ77Z---D",
"population": 4000000,
"isCapital": True,
"loc": [
48.8567,
2.3508
]
}
]
},
{
"edges": [
{
"_key": "6392843",
"_id": "internationalHighway/6392843",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Lyon",
"_rev": "_WmZ77pa--B",
"distance": 1300
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Lyon",
"_id": "frenchCity/Lyon",
"_rev": "_WmZ77Z---B",
"population": 80000,
"isCapital": False,
"loc": [
45.76,
4.84
]
}
]
}
]

graph_name = 'amazing'

g = Digraph(graph_name, filename=graph_name, format='jpeg', engine='neato')
g.attr(scale='2', label='Look at my graph my graph is amazing!', fontsize='18')
g.attr('node', shape='circle', fixedsize='true', width='1')

for item in arango_graph:
for vertex in item['vertices']:
g.node(vertex['_id'], label=vertex['_key'])
for edge in item['edges']:
g.edge(edge['_from'], edge['_to'], label=str(edge['distance']))

# Render to file into some directory
g.render(directory='/tmp/', filename=graph_name)
# Or just show rendered file using system default program
g.view()

只有 3 行代码用于自定义,还有 5 行用于提供图形可视化渲染器。请注意,Arango Web UI 不会渲染同一对节点之间的所有边,而 graphviz 会渲染,您可以为每个节点设置不同的样式。

您将需要安装 graphviz 库和 Python 绑定(bind)

第 1 步:安装库

假设您的机器是 Ubuntu:

sudo apt install graphviz

第 2 步:获取 Python 绑定(bind)

pipenv install graphviz

如果您还没有使用 Pipenv你可以用旧的Pip安装:

pip install graphviz

第 3 步:运行示例并享受

Amazing graph, rendered in JPEG

关于python - ArangoDB pyArango 的图形绘图 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49614427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com