Python 使用connectionStyle从networkx绘制的多有向图边
是否可以在相同的节点上使用不同的曲率绘制不同的边 我编写了以下代码,但三条边都重叠了:Python 使用connectionStyle从networkx绘制的多有向图边,python,matplotlib,networkx,graph-theory,edges,Python,Matplotlib,Networkx,Graph Theory,Edges,是否可以在相同的节点上使用不同的曲率绘制不同的边 我编写了以下代码,但三条边都重叠了: import networkx as nx import matplotlib.pyplot as plt G = nx.MultiDiGraph() G.add_node('n1') G.add_node('n2') G.add_edge('n1', 'n2', 0) G.add_edge('n1', 'n2', 1) G.add_edge('n1', 'n2', 2) pos = nx.spring_
import networkx as nx
import matplotlib.pyplot as plt
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
G.add_edge('n1', 'n2', 0)
G.add_edge('n1', 'n2', 1)
G.add_edge('n1', 'n2', 2)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, connectionstyle='arc3, rad = 0.3')
plt.show()
这可以通过使用不同的
rad
参数绘制每条边来完成-如图所示。请注意,我在这里使用的方法是f-strings,它需要Python3.6——在下面,您必须使用不同的方法构建字符串
代码:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
G.add_edge('n1', 'n2', rad=0.1)
G.add_edge('n1', 'n2', rad=0.2)
G.add_edge('n1', 'n2', rad=0.3)
plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], connectionstyle=f'arc3, rad = {edge[2]["rad"]}')
plt.show()
import networkx as nx
import matplotlib.pyplot as plt
def new_add_edge(G, a, b):
if (a, b) in G.edges:
max_rad = max(x[2]['rad'] for x in G.edges(data=True) if sorted(x[:2]) == sorted([a,b]))
else:
max_rad = 0
G.add_edge(a, b, rad=max_rad+0.1)
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
for i in range(5):
new_add_edge(G, 'n1', 'n2')
for i in range(5):
new_add_edge(G, 'n2', 'n1')
plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], connectionstyle=f'arc3, rad = {edge[2]["rad"]}')
plt.show()
输出:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
G.add_edge('n1', 'n2', rad=0.1)
G.add_edge('n1', 'n2', rad=0.2)
G.add_edge('n1', 'n2', rad=0.3)
plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], connectionstyle=f'arc3, rad = {edge[2]["rad"]}')
plt.show()
import networkx as nx
import matplotlib.pyplot as plt
def new_add_edge(G, a, b):
if (a, b) in G.edges:
max_rad = max(x[2]['rad'] for x in G.edges(data=True) if sorted(x[:2]) == sorted([a,b]))
else:
max_rad = 0
G.add_edge(a, b, rad=max_rad+0.1)
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
for i in range(5):
new_add_edge(G, 'n1', 'n2')
for i in range(5):
new_add_edge(G, 'n2', 'n1')
plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], connectionstyle=f'arc3, rad = {edge[2]["rad"]}')
plt.show()
我们甚至可以为我们创建一个新函数:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
G.add_edge('n1', 'n2', rad=0.1)
G.add_edge('n1', 'n2', rad=0.2)
G.add_edge('n1', 'n2', rad=0.3)
plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], connectionstyle=f'arc3, rad = {edge[2]["rad"]}')
plt.show()
import networkx as nx
import matplotlib.pyplot as plt
def new_add_edge(G, a, b):
if (a, b) in G.edges:
max_rad = max(x[2]['rad'] for x in G.edges(data=True) if sorted(x[:2]) == sorted([a,b]))
else:
max_rad = 0
G.add_edge(a, b, rad=max_rad+0.1)
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
for i in range(5):
new_add_edge(G, 'n1', 'n2')
for i in range(5):
new_add_edge(G, 'n2', 'n1')
plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], connectionstyle=f'arc3, rad = {edge[2]["rad"]}')
plt.show()
输出:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
G.add_edge('n1', 'n2', rad=0.1)
G.add_edge('n1', 'n2', rad=0.2)
G.add_edge('n1', 'n2', rad=0.3)
plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], connectionstyle=f'arc3, rad = {edge[2]["rad"]}')
plt.show()
import networkx as nx
import matplotlib.pyplot as plt
def new_add_edge(G, a, b):
if (a, b) in G.edges:
max_rad = max(x[2]['rad'] for x in G.edges(data=True) if sorted(x[:2]) == sorted([a,b]))
else:
max_rad = 0
G.add_edge(a, b, rad=max_rad+0.1)
G = nx.MultiDiGraph()
G.add_node('n1')
G.add_node('n2')
for i in range(5):
new_add_edge(G, 'n1', 'n2')
for i in range(5):
new_add_edge(G, 'n2', 'n1')
plt.figure(figsize=(6,6))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], connectionstyle=f'arc3, rad = {edge[2]["rad"]}')
plt.show()
这太棒了!工作起来很有魅力!在这个例子中,我们怎么能有边缘标签呢?我的问题与@MichelGokanKhan相同。如何添加边标签。