【问题标题】:Python networkx: node are not taking color listPython networkx:节点不采用颜色列表
【发布时间】:2016-04-27 16:33:52
【问题描述】:

我开始学习 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()

【问题讨论】:

    标签: python networkx


    【解决方案1】:

    您在调用 nx.draw() 时有错字。解释器没有发现这个错字,因为您可以将任何您想要的关键字参数传递给函数,只是某些关键字会被执行/使用。

    【讨论】:

      【解决方案2】:

      nx.draw() 的参数列表中有错字。应该是node_color,即

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

      【讨论】:

      • 非常感谢,我花了一整天的时间试图弄清楚,但我看不到!!为什么python解释器没有检测到?
      猜你喜欢
      • 2021-04-30
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多