1

我正在尝试使用 networkx 作为网络图来可视化数据。我的数据看起来不错,但我想添加悬停和点击事件以显示其他信息。例如,可能有一个名为“纽约”的节点,单击时会在画布的一侧显示一个小表格,其中提供诸如多少个城市、当前人口等信息。我目前正在使用带有 networkx 的 pyviz。就创建图表而言,这似乎非常简单,但在我正在寻找的用户交互类型上却不是那么简单。

我也尝试了散景和绘图,但是在工作时点击和悬停功能,用networkx实现并不是很简单。这是我的图表的样子。我的目标是展示系统之间的关系。

pyvis 图

4

2 回答 2

2

我维护了一个名为netgraph的用于网络可视化的 python 库,它可以很好地与 networkx 或 igraphGraph对象配合使用。我认为这是一个功能的好主意,所以我只是在dev分支上实现了一个简单的框架版本。

演示 gif

通过 pip 安装:

pip install https://github.com/paulbrodersen/netgraph/archive/dev.zip

重现上述示例的代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx

from netgraph import InteractiveGraph

g = nx.cubical_graph()

tables = dict()
for node in g.nodes:
    data = np.round(np.random.rand(3,2), decimals=2)
    table = pd.DataFrame(data, index=['Lorem', 'ipsum', 'dolor'], columns=['sit', 'amet'])
    tables[node] = table

for edge in g.edges:
    data = np.round(np.random.rand(4,1), decimals=2)
    table = pd.DataFrame(data, index=['consectetur', 'adipiscing', 'elit', 'Mauris'], columns=['sed'])
    tables[edge] = table

fig, ax = plt.subplots(figsize=(12,5))
fig.subplots_adjust(right=0.6) # make space for table on the right
bbox = [1.5, 0.1, 0.5, 0.8] # position of the table in axes coordinates
instance = InteractiveGraph(g, node_labels=True, tables=tables, table_kwargs=dict(edges='horizontal', fontsize=16, bbox=bbox), ax=ax)
plt.show()
于 2021-12-10T16:07:52.973 回答
1

Take a look at the kglab project which is an open source abstraction layer in Python that integrates both NetworkX and PyVis, along with other graph related libraries in Python. It was built for this kind of use case.

There's a class kglab.KnowledgeGraph which has transforms and inverse transforms to work these other libraries:

For instance, you could:

  1. build a graph using a KnowledgeGraph object
  2. transform out to run NetworkX graph algorithms
  3. use an inverse transform to populate calculated attributes on the main graph object
  4. transform out to load and run a PyVis interactive session, which in turn can have clickable components

We've got Jupyter notebooks on the GH repo showing each of these steps. plus a developer community where other people can help for a specific use case (create a GH issue)

于 2021-12-08T21:53:46.850 回答