我有一个使用Networkx创建并使用Mayavi绘制的图形网络。
创建图后,我将使用G.remove_nodes_from()
删除度为<2的节点。删除节点后,连接到这些节点的边将被删除,但这些节点仍会出现在最终输出中(下图)。
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)
中删除已删除节点的位置。
但是我仍然没有得到正确的输出。
答案 0 :(得分:1)
这是Mayavi中交互式删除网络节点和边缘的解决方案 (我认为matplotlib可能足够且容易,但无论如何...)。
该解决方案的灵感来自this Mayavi example。
但是,该示例不能直接转移,因为字形(用于可视化节点)由许多点组成,并且在绘制时
每个字形/节点本身,point_id
不能用于标识字形/节点。而且,它不包括
隐藏/删除对象。为了避免这些问题,我使用了四个想法:
每个节点/边缘都作为一个单独的对象绘制,因此更容易调整其(可见性)属性。
单击而不隐藏节点/边,而是将其隐藏。 此外,单击两次可使节点再次可见 (这不适用于以下代码的边缘,但是您可以根据需要实现该功能, 只需跟踪可见节点即可)。 可见的节点可以在最后收集(请参见下面的代码)。
如示例中所示,鼠标位置是使用选择器回调捕获的。
但不是直接使用最接近点的point_id
,而是直接使用其坐标。
要删除/隐藏的节点是通过计算鼠标位置与所有节点之间的最小欧几里得距离来找到的。
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()]