【问题标题】:Colour and weights in a network网络中的颜色和权重
【发布时间】:2020-12-08 02:19:15
【问题描述】:

我想创建一个图表,其中节点根据列中的值具有不同的权重和颜色。

数据样本是

Node Weight Colour Neighbours
1 23 red [3,5]
3 18 blue [2]
4 50 blue []
5 18 blue [1]
2 36 green [3]

上表按节点显示链接:

  • 节点 1 与 3 和 5 链接。它的权重为 23,颜色为红色
  • 节点 2 与 3 链接。它的权重为 36,颜色为绿色
  • 节点 3 与 2 链接。它的权重为 18,颜色为蓝色
  • 节点 4 没有链接。它的颜色为蓝色,重量为 50
  • 节点 5 与 1 链接。它的权重为 18,颜色为蓝色

为了构建网络,我做了如下操作

d = dict(df.drop_duplicates(subset=['Node','Colour'])[['Node','Colour']].to_numpy().tolist())

nodes = G.nodes()
plt.figure(figsize=(30,30)) 
pos = nx.draw(G, with_labels=True, 
              nodelist=nodes,
              node_color=[d.get(i,'lightgreen') for i in nodes], 
              node_size=1000) 

不幸的是颜色都错了!另外,我很难添加有关权重的信息。 节点之间的关系应该分配给它们的权重。 我已经尝试在nx.draw 中使用edge_attr='weight',其中'weight' = df['Weight']。 我希望你能给我一些帮助,让我知道我做错了什么。

【问题讨论】:

    标签: python networkx


    【解决方案1】:
    node_color=[d.get(i,'lightgreen') for i in nodes], 
    

    这种着色方法不起作用,因为您根据 nodes 而不是它们的颜色分配颜色。

    “节点之间的关系应该分配权重。我在 nx.draw 中尝试使用 edge_attr='weight',其中 'weight' = df['Weight']。”*

    对于我提出的解决方案,只有节点有权重而不是边。如果您假装这样做,请在数据框中添加一列,例如:

    Node Weight Colour Neighbours Edge_Weights
    1 23 red [3,5] [w1_3,w1_5]
    3 18 blue [2] [w3_2]
    4 50 blue [] []
    5 18 blue [1] [w5_1]
    2 36 green [3] [w2_3]

    接下来,使用G.add_edge(n1,n2,wight=w) 添加边权重,这里是一些documentation

    由于您有多个要添加到节点的属性,我建议您使用例如df.itertuples() 迭代您的数据框。

    这里是完整的代码:

    df = pd.DataFrame(  data = {'Node': [1, 3, 4, 5, 2], 
                            'Weight': [23, 18 ,50, 18, 36], 
                            'Colour': ["red", "blue", "blue", "blue", "green"], 
                            'Neighbors': [[3,5], [2], [], [1], [3]]
                            }
                  )
       
    
    NROWS = None
    def get_graph_from_pandas(df:
        
        G = nx.DiGraph() # assuming the graph is directed since e.g node 1 has 
                         # 3 as neighbour but 3 doesnt have 1 as neighbour
        
        
        for row in df.itertuples(): # row is the row of the dataframe
            n = row.Node   # change to row.(name of column with the node names)
            w = row.Weight # change to row.(name of column with the node weights)
            c = row.Colour # change to row.(name of column with the node colors)
            neighbors = row.Neighbors
            
            G.add_node(n, weight = w, colour = c)
            
            for neigh in neighbors:
                #add edge weights here, attribute of G.add_edge
                G.add_edge(n,neigh)  
                
        return G
            
            
            
    G = get_graph_from_pandas(df)
    
    print("Done.")
    print("Total number of nodes: ", G.number_of_nodes())
    print("Total number of edges: ", G.number_of_edges())
    
    pos = nx.draw(G, with_labels=True, 
                  node_color=[node[1]['colour'] for node in G.nodes(data=True)], 
                  node_size=200)
    

    【讨论】:

    • 您好,谢谢您的回答。您能否解释一下这行代码:node_color=[node[1]['colour'] for node in G.nodes(data=True)] 我收到错误:KeyError: 'colour' 当我将图形扩展到整个数据集时。非常感谢
    • 嗨!只要你这样做:c = row.Colour, G.add_node(n, weight = w, colour = c) 它不应该返回那个错误
    • node_color=[node[1]['colour'] for node in G.nodes(data=True)] 这一行使用list comprehension 返回一个包含节点颜色的列表。
    • 我相信创建一个新帖子是最好的选择
    • Np @Val ;) 很高兴我能帮上忙
    猜你喜欢
    • 2010-10-17
    • 1970-01-01
    • 2021-04-05
    • 2021-08-19
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2011-02-04
    相关资源
    最近更新 更多