更新时间:2023-01-28 11:43:31
颜色本身并没有错.您只是想为每对最有可能是边缘着色的节点分配一种颜色.在这种情况下,您需要将 node_color
关键字替换为 edge_color
.
Colors itself are not wrong. You're just trying to assign a color to each couple of nodes which is most likely to be coloring of edges. In that case you need to replace node_color
keyword with edge_color
.
G = nx.from_pandas_edgelist(df, 'User', 'Val')
colors = df[["User", "Color"]].drop_duplicates()["Color"]
plt.figure(figsize=(3, 3))
pos = nx.spring_layout(G)
nx.draw(G, edge_color = colors, pos = pos)
net = nx.draw_networkx_labels(G, pos = pos)
plt.show()
这是我正在使用的 df
:
t = '''
User Val Color
92 Laura NaN red
100 Laura John red
148 Laura Mike red
168 Laura Mirk red
293 Laura Sara red
313 Laura Sim red
440 Martyn Pierre orange
440 Martyn Hugh orange
440 Martyn Lauren orange
440 Martyn Sim orange'''
from io import StringIO
df = pd.read_csv(StringIO(t), sep='\s+')