Python networkx:节点未采用颜色列表

Python networkx:节点未采用颜色列表,python,networkx,Python,Networkx,我开始学习Python使用networkx。我正在尝试构建一个以某个节点(节点0)为根的层次结构,并希望颜色根据与该节点的距离(以跳数为单位)进行更改。我已经写了下面的代码,但我不知道为什么颜色没有改变!!你能指出问题所在吗 import networkx as nx import matplotlib.pyplot as plt import matplotlib.cm as cmx import random # initial values of rings in nodes MAX_R

我开始学习Python使用networkx。我正在尝试构建一个以某个节点(节点0)为根的层次结构,并希望颜色根据与该节点的距离(以跳数为单位)进行更改。我已经写了下面的代码,但我不知道为什么颜色没有改变!!你能指出问题所在吗

import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
import random

# initial values of rings in nodes
MAX_RING = 1000

class Node:
    count = int(0)
    tx_range = 3
    def __init__(self, i):
        Node.count+= 1
        self.id = int(i)
        self.ring = int(MAX_RING)
        self.x_pos = int()
        self.y_pos = int()
        self.parent_list = list()
        self.nbr_list = list()
        self.color = float()


    def addNbr(self, nbr):
        self.nbr_list.append(nbr)

def createNodes(Nodes, pos, labels):
    for i in range(num_of_nodes):
        Nodes.append(Node(i))
        Nodes[i].x_pos = random.randrange(net_width)
        Nodes[i].y_pos = random.randrange(net_hieght)
        pos[i] = (Nodes[i].x_pos, Nodes[i].y_pos)
        labels[i] = i


def createEdges(Graph, Nodes, Edges):
    for i in range(num_of_nodes):
        for j in range(i + 1, num_of_nodes):
            dist = ((Nodes[i].x_pos - Nodes[j].x_pos)**2 + (Nodes[i].y_pos - Nodes[j].y_pos)**2) ** 0.5
            if dist <= Node.tx_range:
                Graph.add_edge(i, j)
                #Graph.add_edge(j, i)
                Edges.append((i , j))
                Nodes[i].addNbr(Nodes[j])
                Nodes[j].addNbr(Nodes[i])
                print("Edge added")

def createRings(G, Nodes, colors, VisEdges):
    # Base station is node 0 and has ring 0
    # find all nodes in current ring and check their neighbors to create the next ring
    # next ring becomes current ring and repeat from previous step
    current_ring = 0
    current_node = Nodes[0]
    current_node.ring = current_ring
    # temporary set to travers all nodes initially contains the BS
    N = [Nodes[0]]
    while len(N) > 0:
        node = N.pop(0)
        for nbr in node.nbr_list:
            # for each node check if it has a neighbor with ring less than MAX_RING
            if nbr.ring > node.ring + 1:
                if(nbr.ring != MAX_RING):
                    print('CHANGING PARENT')
                nbr.ring = node.ring + 1
                nbr.color = nbr.ring
                N.append(nbr)
                VisEdges.append((node.id, nbr.id))
    for node in Nodes:
        colors.insert(node.id, node.color)




def main():
    Nodes = list()
    Edges = list()
    VisEdges = list()
    pos = dict()
    labels = {}
    colors = []
    G = nx.Graph()
    createNodes(Nodes, pos, labels)
    G.add_nodes_from(pos.keys())
    createEdges(G, Nodes, Edges)
    for n, p in pos.items():
        G.node[n]['pos'] = p

    createRings(G, Nodes, colors, VisEdges)
    colors[0] = 1
    for node in Nodes:
        print('Node[',node.id,'] ring is ', node.ring, 'color is', colors[node.id])


    nx.draw(G,pos, edgelist=VisEdges, nodelist=pos.keys() , node_colors=colors, cmap=cmx.get_cmap('Reds'),
            vmin=0, vmax=max(colors))

    nx.draw_networkx_labels(G,pos,labels,font_size=10)
    plt.show()



num_of_nodes = 100
net_width = 15
net_hieght = 15
main()
将networkx导入为nx
将matplotlib.pyplot作为plt导入
将matplotlib.cm作为cmx导入
随机输入
#节点环的初值
最大环=1000
类节点:
计数=int(0)
tx_范围=3
定义初始化(self,i):
Node.count+=1
self.id=int(i)
self.ring=int(最大环)
self.x_pos=int()
self.y_pos=int()
self.parent_list=list()
self.nbr_list=list()
self.color=float()
def addNbr(自身,nbr):
self.nbr\u list.append(nbr)
def createNodes(节点、位置、标签):
对于范围内的i(节点数):
Nodes.append(节点(i))
节点[i].x_pos=random.randrange(净宽度)
节点[i].y\u pos=random.randrange(净高度)
pos[i]=(节点[i].x_pos,节点[i].y_pos)
标签[i]=i
def CreateEdge(图形、节点、边):
对于范围内的i(节点数):
对于范围内的j(i+1,节点数):
dist=((节点[i].x_pos-Nodes[j].x_pos)**2+(节点[i].y_pos-Nodes[j].y_pos)**2)**0.5
如果距离为0:
node=N.pop(0)
对于node.nbr\u列表中的nbr:
#对于每个节点,检查其是否有一个环小于MAX_环的邻居
如果nbr.ring>node.ring+1:
如果(丁腈橡胶环!=最大环):
打印('更改父项')
nbr.ring=节点.ring+1
nbr.color=nbr.ring
N.附加(nbr)
VisEdges.append((node.id,nbr.id))
对于节点中的节点:
颜色.插入(node.id,node.color)
def main():
节点=列表()
边=列表()
VisEdges=列表()
pos=dict()
标签={}
颜色=[]
G=nx.Graph()
createNodes(节点、位置、标签)
G.从(位置键())添加节点
CreateEdge(G、节点、边)
对于位置项()中的n、p:
G.node[n]['pos']=p
创建环(G、节点、颜色、虎钳)
颜色[0]=1
对于节点中的节点:
打印('Node[',Node.id',]ring is',Node.ring',color is',colors[Node.id])
nx.draw(G,pos,edgelist=VisEdges,nodelist=pos.keys(),node_colors=colors,cmap=cmx.get_cmap('Reds'),
vmin=0,vmax=max(颜色))
nx.绘制网络X标签(G、pos、标签、字体大小=10)
plt.show()
节点数=100
净宽度=15
净高度=15
main()

您的参数列表中有一个输入错误,
nx.draw()
。它应该是
node\u color
,即

nx.draw(G,pos, edgelist=VisEdges, nodelist=pos.keys() , node_color=colors, cmap=cmx.get_cmap('Reds'),
        vmin=0, vmax=max(colors))

您对nx.draw()的调用中有一个输入错误。解释器没有发现这个输入错误,因为你可以向函数传递你想要的任何关键字参数,只是某些关键字被使用了。

非常感谢,我花了一整天的时间试图找出它,但我看不见!!为什么python解释器没有检测到它?