- 首先,在Jupyter Notebook中确保安装了"matplotlib"和"networkx"库,这两个库分别用于绘制图形和构建图形。
- 然后,在查询中添加以下代码来构建图形:
import matplotlib.pyplot as plt
import networkx as nx
# 首先,使用Gremlin查询检索顶点和边
vertices = g.V().hasLabel('person')
edges = g.E().hasLabel('knows')
# 创建一个空的图形对象
graph = nx.Graph()
# 将顶点和边添加到图形对象
for vertex in vertices:
graph.add_node(vertex.id, label=vertex.valueMap()['name'][0])
for edge in edges:
graph.add_edge(edge.outV().id, edge.inV().id, label=edge.label)
# 将构建的图形对象绘制出来
pos = nx.spring_layout(graph)
nx.draw_networkx_nodes(graph, pos)
nx.draw_networkx_edges(graph, pos)
nx.draw_networkx_edge_labels(graph, pos)
nx.draw_networkx_labels(graph, pos)
plt.axis('off')
plt.show()
- 运行代码后,将会在Jupyter Notebook中显示绘制出的图形。