Skip to main content
 首页 » 编程设计

python之Networkx 情节 : Make edges stay out of the nodes

2024年11月24日8zdz8207

我想在 python 中使用 networkx 绘制有向网络图。当使用不同于 1 的 alpha 值时,边缘的起点也在节点内绘制;但是箭头很好。

如何让边远离我的节点?

我没有在文档中找到任何关于它的信息。设置 alpha=1 显然可以解决问题,但这不是我想要的。

import math 
import pandas as pd 
import numpy as np 
import networkx as nx 
import matplotlib.pyplot as plt 
 
pos={"x":(1/2, math.sqrt(3/4)), "y":(0,0), "z":(1,0)} 
 
G=nx.DiGraph() 
G.add_edge("x", "y") 
G.add_edge("x", "z") 
nx.draw(G, pos=pos, with_labels=True, node_size=1500, alpha=0.3, arrows=True, 
        arrowsize=20, width=2) 
plt.title("Direct link") 
plt.show() 

这是出来的。边缘继续进入“x”节点,这是不好的。

请您参考如下方法:

您可以通过多次绘制节点来解决此问题:

import math 
import networkx as nx 
import matplotlib.pyplot as plt 
 
pos={"x":(1/2, math.sqrt(3/4)), "y":(0,0), "z":(1,0)} 
 
G=nx.DiGraph() 
G.add_edge("x", "y") 
G.add_edge("x", "z") 
nx.draw_networkx_edges(G, pos=pos, with_labels=True, node_size=1500, alpha=0.3, arrows=True, 
        arrowsize=20, width=2) 
# draw white circles over the lines 
nx.draw_networkx_nodes(G, pos=pos, with_labels=True, node_size=1500, alpha=1, arrows=True, 
        arrowsize=20, width=2, node_color='w') 
# draw the nodes as desired 
nx.draw_networkx_nodes(G, pos=pos, node_size=1500, alpha=.3, arrows=True, 
        arrowsize=20, width=2) 
nx.draw_networkx_labels(G, pos=pos) 
plt.title("Direct link") 
plt.axis("off") 
plt.show()