我试图弄清楚如何在我的代码示例中增加边缘权重,但是我找不到与wxPython结合使用的任何好的文档。
我想要边缘,可以这么说,节点的距离就像在我的列表“ edge_weight”中一样。标签和其他所有东西都按预期工作。
作为一个很小的添加,是否可以添加诸如阴影效果之类的内容或类似于主节点的任何内容以使其突出显示?
我的代码如下所示:
import wx
import matplotlib.pyplot as plt
import networkx as nx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
class NetworkFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1)
self.SetSize(wx.Size(1280, 768))
self.panel = wx.Panel(self)
self.fig = plt.figure()
self.canvas = FigCanvas(self.panel, -1, self.fig)
G = nx.Graph()
nodes = [0,1,2,3,4,5,6,7]
node_sizes = [6500,4000,4000,2500,2500,2500,2500,2500]
node_color = ["#00d992","#00d9c8","#00d9c8","#00b4d9","#00b4d9","#00b4d9","#00b4d9","#00b4d9"]
edges = [(1,0),(2,0),(3,1),(4,1),(5,1),(6,2),(7,2)]
node_label = {0: "Printer",
1: "Case",
2: "Electronics",
3: "Plastic 1",
4: "Plastic 2",
5: "Plastic 3",
6: "Metal 1",
7: "Metal 2"}
edge_weights = [1, 0.5, 0.5, 0.1, 0.1, 0.1, 0.1, 0.1] # ?????
G.add_nodes_from(nodes)
G.add_edges_from(edges)
nx.draw(G, node_size = node_sizes, node_color = node_color, labels = node_label, with_labels=True)
plt.axis('off')
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.panel.SetSizer(self.vbox)
if __name__ == '__main__':
app = wx.App()
app.frame = NetworkFrame()
app.frame.Show()
app.MainLoop()
根据您问题中提供的信息,我做出了以下假设:
现在让我们从第一个开始。
1.根据边缘权重绘制边缘长度您可以将pygraphviz与networkx结合使用,以根据您的选择来选择边缘长度,从而获得图形的布局。
为此,首先需要缩放边缘权重,以使节点之间的距离在图形中可见(假设最小值为0.1
,最大值为1
,则边缘将不可见)。我已经将它们缩放了10,但是您可以使用任何适合您的缩放方法。
new_edge_weights = [ int(x*10) for x in edge_weights]
接下来,创建字典,键为“ len”(pygraphviz将使用此属性,请参见此处),值作为边缘长度
lengths = {}
for e,l in zip(edges, new_edge_weights):
lengths[e] = dict(len=l)
# {(1, 0): {'len': 10},
# (2, 0): {'len': 5},
# (3, 1): {'len': 5},
# (4, 1): {'len': 1},
# (5, 1): {'len': 1},
# (6, 2): {'len': 1},
# (7, 2): {'len': 1}}
现在使用graphviz_layout获取节点的位置并绘制图形
pos = graphviz_layout(G, prog='neato')
nx.draw_networkx(G, pos=pos,
node_size = node_sizes,
node_color = node_color,
edge_weights='len',
labels = node_label,
with_labels=True)
2.突出显示您的主节点(假设主节点是node 0: Printer
。这可能会有些棘手。因此,一种方法是在主节点本身上绘制另一个较小尺寸和颜色的节点。
# Set the nodel label to empty for the new node
node_label[len(node_label)+1] = ""
# Set the size of the new node smaller than the master node
master_idx = 0
node_sizes.append(node_sizes[master_idx]-300)
# change the master node color to black
node_color[master_idx] = "#000000"
# set the color of the inner node (new smaller node)
node_color.append("#00d992")
# Set the position of the node same as the master node
pos[len(pos)+1] = pos[master_idx]
# Add node to the graph
G.add_node(len(node_label))
# Draw the graph
nx.draw_networkx(G,
pos=pos,
node_size = node_sizes,
node_color = node_color,
edge_weights='len',
labels = node_label,
with_labels=True)
。
请注意,我已缩放节点大小以使边缘可见。您可以根据需要进行更改,并相应地调整步长。
Here is the full program
import wx
import matplotlib.pyplot as plt
import networkx as nx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
from networkx.drawing.nx_agraph import graphviz_layout
class NetworkFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1)
self.SetSize(wx.Size(1280, 768))
self.panel = wx.Panel(self)
self.fig = plt.figure()
self.canvas = FigCanvas(self.panel, -1, self.fig)
G = nx.Graph()
nodes = [0,1,2,3,4,5,6,7]
node_sizes = [650,400,400,250,250,250,250,250]
node_sizes = [x+1500 for x in node_sizes]
node_color = ["#00d992","#00d9c8","#00d9c8","#00b4d9","#00b4d9","#00b4d9","#00b4d9","#00b4d9"]
edges = [(1,0),(2,0),(3,1),(4,1),(5,1),(6,2),(7,2)]
node_label = {0: "Printer",
1: "Case",
2: "Electronics",
3: "Plastic 1",
4: "Plastic 2",
5: "Plastic 3",
6: "Metal 1",
7: "Metal 2"}
edge_weights = [1, 0.5, 0.5, 0.1, 0.1, 0.1, 0.1, 0.1] # ?????
new_edge_weights = [ int(x*10) for x in edge_weights]
lengths = {}
for e,l in zip(edges, new_edge_weights):
lengths[e] = dict(len=l)
G.add_nodes_from(nodes)
G.add_edges_from(edges)
nx.set_edge_attributes(G, lengths)
pos = graphviz_layout(G, prog='neato')
# Set the nodel label to empty for the new node
node_label[len(node_label)+1] = ""
# Set the size of the new node smaller than the master node
master_idx = 0
node_sizes.append(node_sizes[master_idx]-300)
# change the master node color to black
node_color[master_idx] = "#000000"
# set the color of the inner node (new smaller node)
node_color.append("#00d992")
# Set the position of the node same as the master node
pos[len(pos)+1] = pos[master_idx]
# Add node to the graph
G.add_node(len(node_label))
# Draw the graph
nx.draw_networkx(G,
pos=pos,
node_size = node_sizes,
node_color = node_color,
edge_weights='len',
labels = node_label,
with_labels=True)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.panel.SetSizer(self.vbox)
if __name__ == '__main__':
app = wx.App()
app.frame = NetworkFrame()
app.frame.Show()
app.MainLoop()
参考文献
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句