3

我有一个使用 Networkx 创建并使用 Mayavi 绘制的图形网络。

创建图表后,我将删除度数 < 2 的节点,使用G.remove_nodes_from(). 删除节点后,连接到这些节点的边将被删除,但节点仍会出现在最终输出中(下图)。

import matplotlib.pyplot as plt
from mayavi import mlab
import numpy as np
import pandas as pd
    
    
pos = [[0.1, 2, 0.3], [40, 0.5, -10],
       [0.1, -40, 0.3], [-49, 0.1, 2],
       [10.3, 0.3, 0.4], [-109, 0.3, 0.4]]
pos = pd.DataFrame(pos, columns=['x', 'y', 'z'])
    
ed_ls = [(x, y) for x, y in zip(range(0, 5), range(1, 6))]
    
G = nx.Graph()
G.add_edges_from(ed_ls)
remove = [node for node, degree in dict(G.degree()).items() if degree < 2]
G.remove_nodes_from(remove)
pos.drop(pos.index[remove], inplace=True)

print(G.edges)
    
nx.draw(G)
plt.show()
    
mlab.figure(1, bgcolor=bgcolor)
mlab.clf()
    
for i, e in enumerate(G.edges()):
    
# ----------------------------------------------------------------------------
    # the x,y, and z co-ordinates are here
    pts = mlab.points3d(pos['x'], pos['y'], pos['z'],
                        scale_mode='none',
                        scale_factor=1)
# ----------------------------------------------------------------------------
    pts.mlab_source.dataset.lines = np.array(G.edges())
    tube = mlab.pipeline.tube(pts, tube_radius=edge_size)
    
    mlab.pipeline.surface(tube, color=edge_color)
    
mlab.show()  # interactive window

在此处输入图像描述

我想就如何删除已删除的节点和相应的位置并在输出中显示其余部分提出建议。

其次,我想知道如何以交互方式删除节点和连接到这些节点的边。例如,如果我想删除连接到度数 < 2 的节点的节点和边,首先我想显示一个交互式图形,其中所有度数 < 2 的节点都突出显示。用户可以以交互方式选择需要删除的节点。通过单击突出显示的节点,可以删除节点和连接边。

编辑:我试图通过在上面发布的完整代码中pos包含更新来从数据框中删除已删除节点的位置。pos.drop(pos.index[remove], inplace=True)

但我仍然没有得到正确的输出。

在此处输入图像描述

4

1 回答 1

2

这是在 Mayavi 中交互式删除网络节点和边缘的解决方案(我认为 matplotlib 可能足够且更容易,但无论如何......)。

该解决方案的灵感来自这个 Mayavi 示例。但是,该示例不能直接转移,因为字形(用于可视化节点)由许多点组成,并且在单独绘制每个字形/节点时,point_id不能用于识别字形/节点。此外,它不包括隐藏/删除对象的选项。为了避免这些问题,我使用了四个想法:

  1. 每个节点/边都被绘制为一个单独的对象,因此更容易调整它的(可见性)属性。

  2. 不是删除节点/边,而是在单击时隐藏它们。此外,单击两次使节点再次可见(这不适用于使用下面的代码的边缘,但如果需要,您可以实现它,只需要跟踪可见节点)。最后可以收集可见节点(参见下面的代码)。

  3. 如示例所示,鼠标位置是使用选取器回调捕获的。但不是使用point_id最近点的,而是直接使用它的坐标。

  4. 通过计算鼠标位置和所有节点之间的最小欧几里得距离来找到要删除/隐藏的节点。

PS:在您的原始代码中,for 循环非常多余,因为它将所有节点和边多次绘制在彼此之上。

希望有帮助!

在此处输入图像描述

# import modules
from mayavi import mlab
import numpy as np
import pandas as pd
import networkx as nx

# set number of nodes
number = 6

# create random node positions
np.random.seed(5)
pos = 100*np.random.rand(6, 3)
pos = pd.DataFrame(pos, columns=['x', 'y', 'z'])

# create chain graph links
links = [(x, y) for x, y in zip(range(0, number-1), range(1, number))]

# create graph (not strictly needed, link list above would be enough)
graph = nx.Graph()
graph.add_edges_from(links)

# setup mayavi figure
figure = mlab.gcf()
mlab.clf()

# add nodes as individual glyphs
# store glyphs in dictionary to allow interactive adjustments of visibility
color = (0.5, 0.0, 0.5)
nodes = dict()
texts = dict()
for ni, n in enumerate(graph.nodes()):
    xyz = pos.loc[n]
    n = mlab.points3d(xyz['x'], xyz['y'], xyz['z'], scale_factor=5, color=color)
    label = 'node %s' % ni
    t = mlab.text3d(xyz['x'], xyz['y'], xyz['z']+5, label, scale=(5, 5, 5))
    # each glyph consists of many points
    # arr = n.glyph.glyph_source.glyph_source.output.points.to_array()
    nodes[ni] = n
    texts[ni] = t

# add edges as individual tubes
edges = dict()
for ei, e in enumerate(graph.edges()):
    xyz = pos.loc[np.array(e)]
    edges[ei] = mlab.plot3d(xyz['x'], xyz['y'], xyz['z'], tube_radius=1, color=color)


# define picker callback for figure interaction
def picker_callback(picker):
    # get coordinates of mouse click position
    cen = picker.pick_position
    # compute Euclidean distance btween mouse position and all nodes
    dist = np.linalg.norm(pos-cen, axis=1)
    # get closest node
    ni = np.argmin(dist)
    # hide/show node and text
    n = nodes[ni]
    n.visible = not n.visible
    t = texts[ni]
    t.visible = not t.visible
    # hide/show edges
    # must be adjusted if double-clicking should hide/show both nodes and edges in a reasonable way
    for ei, edge in enumerate(graph.edges()):
        if ni in edge:
            e = edges[ei]
            e.visible = not e.visible


# add picker callback
picker = figure.on_mouse_pick(picker_callback)
picker.tolerance = 0.01

# show interactive window
# mlab.show()

# collect visibility/deletion status of nodes, e.g.
# [(0, True), (1, False), (2, True), (3, True), (4, True), (5, True)]
[(key, node.visible) for key, node in nodes.items()]
于 2020-06-24T13:46:33.487 回答