简体   繁体   English

创建确定的边和节点的图

[英]Creating graph of determined edges and nodes

I want to create a graph that shows connected nodes and edges.我想创建一个显示连接节点和边的图。 I have determined the nodes and edges.我已经确定了节点和边。

import matplotlib.pyplot as plt
import networkx as nx
%matplotlib notebook

G = nx.Graph()

G.add_node(y[i]==1, color="blue", size=500)
G.add_node(y[i]==0, color="green", size=300)
G.add_edge(d[k,i]*w[k,i], color="green")
G.add_edge(d[i,j]*f[i,j]>0, color="blue")

nx.draw(G, with_labels=True)
plt.show()

Here are the nodes and edges that are determined ( W[k,i] is a longer list) W & F are edges, Y are nodes.这里是确定的节点和边(W[k,i] 是一个更长的列表)W & F 是边,Y 是节点。 d[i,j] is the distance. d[i,j] 是距离。 Distance is between nodes with given positions.距离是给定位置的节点之间。

Y[0] 1
Y[1] 1
Y[3] 1
Y[12] 1
Y[57] 1
Y[59] 1
W[22,0] 1
W[23,0] 1
W[24,0] 1
F[0,0] 5
F[0,1] 5
F[1,3] 4
F[3,59] 3
F[3,98] 3.63798e-12
F[8,51] 5.65326e-12
F[24,8] 4.65317e-12
F[25,0] 7.10543e-15
F[25,24] 1.81366e-12
F[51,8] 1.56776e-15
F[51,59] 4.78817e-12
F[57,12] 1
F[59,57] 2

I get this error:我收到此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-314-9036a4e704aa> in <module>
     23 G.add_node(y[i]==1, color="blue", size=500)
     24 G.add_node(y[i]==0, color="green", size=300)
---> 25 G.add_edge(d[k,i]*w[k,i], color="green")
     26 G.add_edge(d[i,j]*f[i,j]>0, color="blue")
     27 

TypeError: add_edge() missing 1 required positional argument: 'v_of_edge'

What do I need to add to make the graph?我需要添加什么来制作图表?

add_edge takes 2 mandatory parameters as an input: u_of_edge and v_of_edge . add_edge将 2 个强制参数作为输入: u_of_edgev_of_edge
In your code在你的代码中

G.add_edge(u_of_edge=d[k,i]*w[k,i], color="green")
             ^^^^^^
G.add_edge(u_of_edge=d[i,j]*f[i,j]>0, color="blue")
             ^^^^^^

you are missing the v_of_edge node, and that's why you are getting the TypeError .您缺少v_of_edge节点,这就是您收到TypeError的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM