英文:
Using pydot with networkx
问题
我试图绘制以下列表,使边的粗细与权重成比例:
g_list = [('Alpha', 'Alpha', 7.06), ('Alpha', 'Bravo', 0.98), ('Alpha', 'Charlie', 0.0), ('Alpha', 'Delta', 0.0), ('Alpha', 'Echo', 1.57), ('Alpha', 'Foxtrot', 2.16), ('Alpha', 'Golf', 1.57), ('Alpha', 'Hotel', 0.39), ('Alpha', 'India', 0.0), ('Alpha', 'Juliet', 0.2), ('Alpha', 'Kilo', 0.59), ('Bravo', 'Alpha', 1.66), ('Bravo', 'Bravo', 8.54), ('Bravo', 'Charlie', 1.21), ('Bravo', 'Delta', 1.78), ('Bravo', 'Echo', 0.25), ('Bravo', 'Foxtrot', 0.76), ('Bravo', 'Golf', 1.66), ('Bravo', 'Hotel', 1.59), ('Bravo', 'India', 2.87), ('Bravo', 'Juliet', 1.72), ('Bravo', 'Kilo', 1.27), ('Charlie', 'Alpha', 1.0), ('Charlie', 'Bravo', 2.5), ('Charlie', 'Charlie', 7.0), ('Charlie', 'Delta', 5.0), ('Charlie', 'Echo', 0.0), ('Charlie', 'Foxtrot', 0.5), ('Charlie', 'Golf', 3.0), ('Charlie', 'Hotel', 0.0), ('Charlie', 'India', 0.5), ('Charlie', 'Juliet', 2.5), ('Charlie', 'Kilo', 1.5)]
以下代码有效,但不够美观:
import networkx as nx
G = nx.Graph()
for i in range(len(g_list)):
if ((g_list[i][0] != g_list[i][1]) and (g_list[i][2] != 0.0)):
G.add_edge(g_list[i][0], g_list[i][1], weight=g_list[i][2])
pos = nx.spring_layout(G)
for edge in G.edges(data='weight'):
nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2])
nx.draw_networkx(G, pos, with_labels=True, arrows=True, arrowstyle='<-', alpha=1, node_color='#ffffff')
plt.axis('off')
plt.savefig('graph.jpg')
所述的演示可以通过使用 pydot 来实现:
G = nx.DiGraph()
for i in range(len(g_list)):
if ((g_list[i][0] != g_list[i][1]) and (g_list[i][2] != 0.0)):
G.add_edge(g_list[i][1], g_list[i][0], weight=g_list[i][2])
p = nx.drawing.nx_pydot.to_pydot(G)
p.write_png('graph.png')
这是一个更美观的图形,但当我尝试添加可变粗细的边缘时,会回到第一个图形。有没有办法将这两种方法结合起来,以获得 pydot 的布局和对网络边缘的绘制控制?我尝试了以下方法,但仍然没有第二幅图中清晰的布局:
pos = nx.nx_pydot.pydot_layout(G, prog='dot')
nx.draw_networkx(G, pos, with_labels=True, arrows=True, arrowstyle='<-', alpha=1, node_color='#ffffff')
for edge in G.edges(data='weight'):
nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2])
结果如下图,但仍然没有第二幅图中的清晰布局:
英文:
I am trying to plot the following list as a graph with edge thickness proportional to the weights.
g_list=[('Alpha', 'Alpha', 7.06), ('Alpha', 'Bravo', 0.98), ('Alpha', 'Charlie', 0.0), ('Alpha', 'Delta', 0.0), ('Alpha', 'Echo', 1.57), ('Alpha', 'Foxtrot', 2.16), ('Alpha', 'Golf', 1.57), ('Alpha', 'Hotel', 0.39), ('Alpha', 'India', 0.0), ('Alpha', 'Juliet', 0.2), ('Alpha', 'Kilo', 0.59), ('Bravo', 'Alpha', 1.66), ('Bravo', 'Bravo', 8.54), ('Bravo', 'Charlie', 1.21), ('Bravo', 'Delta', 1.78), ('Bravo', 'Echo', 0.25), ('Bravo', 'Foxtrot', 0.76), ('Bravo', 'Golf', 1.66), ('Bravo', 'Hotel', 1.59), ('Bravo', 'India', 2.87), ('Bravo', 'Juliet', 1.72), ('Bravo', 'Kilo', 1.27), ('Charlie', 'Alpha', 1.0), ('Charlie', 'Bravo', 2.5), ('Charlie', 'Charlie', 7.0), ('Charlie', 'Delta', 5.0), ('Charlie', 'Echo', 0.0), ('Charlie', 'Foxtrot', 0.5), ('Charlie', 'Golf', 3.0), ('Charlie', 'Hotel', 0.0), ('Charlie', 'India', 0.5), ('Charlie', 'Juliet', 2.5), ('Charlie', 'Kilo', 1.5)]
The following code works but is not pretty
import networkx as nx
G=nx.Graph()
for i in range(len(g_list)):
if((g_list[i][0] != g_list[i][1]) and (g_list[i][2] != 0.0)):
G.add_edge(g_list[i][0],g_list[i][1],weight=g_list[i][2])
pos = nx.spring_layout(G)
for edge in G.edges(data='weight'):
nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2])
nx.draw_networkx(G, pos, with_labels=True, arrows=True, arrowstyle='<-', alpha=1, node_color='#ffffff')
plt.axis('off')
plt.savefig('graph.jpg')
The sort of presentation I'm looking for can be obtained using pydot as folllows
G=nx.DiGraph()
for i in range(len(g_list)):
if((g_list[i][0] != g_list[i][1]) and (g_list[i][2] != 0.0)):
G.add_edge(g_list[i][1],g_list[i][0],weight=g_list[i][2])
p=nx.drawing.nx_pydot.to_pydot(G)
p.write_png('graph.png')
This is a better looking graph but when I try to add the variable thickness edges back using
pos = nx.spring_layout(G)
for edge in G.edges(data='weight'):
nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2])
p=nx.drawing.nx_pydot.to_pydot(G)
I end up with the first graph again. Is there any way of combining the two approaches so that I get the layout of pydot and control over the drawing of the network edges? I have tried the following
pos=nx.nx_pydot.pydot_layout(G, prog='dot')
nx.draw_networkx(G, pos, with_labels=True, arrows=True, arrowstyle='<-', alpha=1, node_color='#ffffff')
for edge in G.edges(data='weight'):
nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2])
with the following result, but still not the clear layout I get in the second graph.
答案1
得分: 1
如果你想要使用GraphViz的dot
来呈现具有不同边线宽度的图形,你需要将weight
转换为GraphViz可以理解的penwidth
属性。
我发现使用实际的权重会使事情变得太厚重,所以下面是一种将权重的平方根作为penwidth
的方法。
请注意,你也可以使用add_weighted_edges_from
来一次性转换你的数据。
import math
import networkx as nx
from networkx.drawing.nx_pydot import to_pydot
g_list = [
("Alpha", "Alpha", 7.06),
("Alpha", "Bravo", 0.98),
("Alpha", "Charlie", 0.0),
("Alpha", "Delta", 0.0),
("Alpha", "Echo", 1.57),
("Alpha", "Foxtrot", 2.16),
("Alpha", "Golf", 1.57),
("Alpha", "Hotel", 0.39),
("Alpha", "India", 0.0),
("Alpha", "Juliet", 0.2),
("Alpha", "Kilo", 0.59),
("Bravo", "Alpha", 1.66),
("Bravo", "Bravo", 8.54),
("Bravo", "Charlie", 1.21),
("Bravo", "Delta", 1.78),
("Bravo", "Echo", 0.25),
("Bravo", "Foxtrot", 0.76),
("Bravo", "Golf", 1.66),
("Bravo", "Hotel", 1.59),
("Bravo", "India", 2.87),
("Bravo", "Juliet", 1.72),
("Bravo", "Kilo", 1.27),
("Charlie", "Alpha", 1.0),
("Charlie", "Bravo", 2.5),
("Charlie", "Charlie", 7.0),
("Charlie", "Delta", 5.0),
("Charlie", "Echo", 0.0),
("Charlie", "Foxtrot", 0.5),
("Charlie", "Golf", 3.0),
("Charlie", "Hotel", 0.0),
("Charlie", "India", 0.5),
("Charlie", "Juliet", 2.5),
("Charlie", "Kilo", 1.5),
]
graph = nx.DiGraph()
# 添加边,但反转方向,删除自环边和权重为零的边
graph.add_weighted_edges_from([(b, a, w) for (a, b, w) in g_list if w > 0 and a != b])
for edge in graph.edges().values():
edge["penwidth"] = round(1 + math.sqrt(edge["weight"]), 2)
p = to_pydot(graph)
p.write_png("graph.png")
输出结果如下:
英文:
If you want to use GraphViz's dot
to render your graph with varying edge line width, you'll need to convert the weight
to a penwidth
attribute that GraphViz understands.
I found using the actual weight made things way too thick, so here's something that takes the square root of the weight.
Note you can use add_weighted_edges_from
to convert your data in one fell swoop, too.
import math
import networkx as nx
from networkx.drawing.nx_pydot import to_pydot
g_list = [
("Alpha", "Alpha", 7.06),
("Alpha", "Bravo", 0.98),
("Alpha", "Charlie", 0.0),
("Alpha", "Delta", 0.0),
("Alpha", "Echo", 1.57),
("Alpha", "Foxtrot", 2.16),
("Alpha", "Golf", 1.57),
("Alpha", "Hotel", 0.39),
("Alpha", "India", 0.0),
("Alpha", "Juliet", 0.2),
("Alpha", "Kilo", 0.59),
("Bravo", "Alpha", 1.66),
("Bravo", "Bravo", 8.54),
("Bravo", "Charlie", 1.21),
("Bravo", "Delta", 1.78),
("Bravo", "Echo", 0.25),
("Bravo", "Foxtrot", 0.76),
("Bravo", "Golf", 1.66),
("Bravo", "Hotel", 1.59),
("Bravo", "India", 2.87),
("Bravo", "Juliet", 1.72),
("Bravo", "Kilo", 1.27),
("Charlie", "Alpha", 1.0),
("Charlie", "Bravo", 2.5),
("Charlie", "Charlie", 7.0),
("Charlie", "Delta", 5.0),
("Charlie", "Echo", 0.0),
("Charlie", "Foxtrot", 0.5),
("Charlie", "Golf", 3.0),
("Charlie", "Hotel", 0.0),
("Charlie", "India", 0.5),
("Charlie", "Juliet", 2.5),
("Charlie", "Kilo", 1.5),
]
graph = nx.DiGraph()
# Add edges, but reverse direction, remove self-loops, and remove zero-weight edges
graph.add_weighted_edges_from([(b, a, w) for (a, b, w) in g_list if w > 0 and a != b])
for edge in graph.edges().values():
edge["penwidth"] = round(1 + math.sqrt(edge["weight"]), 2)
p = to_pydot(graph)
p.write_png("graph.png")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论