【问题标题】:Python: Network Spring Layout with different color nodesPython:具有不同颜色节点的网络 Spring 布局
【发布时间】:2014-03-27 06:26:09
【问题描述】:

我从给定节点创建最短路径的弹簧布局网络。在这种情况下firm1。我想为每个分离度使用不同的颜色。例如,连接firm1 和其他公司的所有第一条边,比如firm2firm3,我想更改firm2firm3 的节点颜色(两者颜色相同)。然后所有从firm2firm3 连接的公司,比如说firm4firm5 我想改变他们的节点颜色。但我不知道如何更改从firm1 开始的每个分离度的节点颜色。这是我的代码:

import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd

graph = nx.Graph()
with open('C:\\file.txt') as f: #Here, I load a text file with two columns indicating the connections between each firm
    for line in f:
        tic_1, tic_2 = line.split()
        graph.add_edge(tic_1, tic_2)

paths_from_1 = nx.shortest_path(graph, "firm1") #I get the shortest path starting from firm1

x = pd.DataFrame(paths_from_1.values()) #I convert the dictionary of the shortest path into a dataframe


tic_0=x[0].tolist() #there are 7 columns in my dataframe x and I convert each columns into a list. tic_0 is a list of `firm1` string
tic_1=x[1].tolist() #tic_1 is list of all the firms directly connected to firm1
tic_2=x[2].tolist() #tic_2 are the firms indirectly connected to firm1 via the firms in tic_1
tic_3=x[3].tolist() #and so on...
tic_4=x[4].tolist()
tic_5=x[5].tolist()
tic_6=x[6].tolist()

l = len(tic_0)
graph = nx.Graph()

for i in range(len(tic_0)):
        graph.add_edge(tic_0[i], tic_1[i]) 
        graph.add_edge(tic_1[i], tic_2[i])
        graph.add_edge(tic_2[i], tic_3[i])
        graph.add_edge(tic_3[i], tic_4[i])
        graph.add_edge(tic_4[i], tic_5[i])
        graph.add_edge(tic_5[i], tic_6[i])

pos = nx.spring_layout(graph_short, iterations=200, k=)
nx.draw(graph_short, pos, font_size='6',)
plt.savefig("network.png")
plt.show()

如何为每个分离度设置不同的颜色节点?换句话说,tic_1 中的所有公司都应该有一个蓝色的节点,tic_2 中的所有公司都有一个黄色的节点颜色,等等。

【问题讨论】:

    标签: python pandas networkx


    【解决方案1】:

    执行此操作的通用方法是从源节点运行最短路径长度算法来分配颜色。这是一个例子:

    import matplotlib.pyplot as plt
    import networkx as nx
    
    G = nx.balanced_tree(2,5)
    length = nx.shortest_path_length(G, source=0)
    nodelist,hops = zip(*length.items())
    positions = nx.graphviz_layout(G, prog='twopi', root=0)
    nx.draw(G, positions, nodelist = nodelist, node_color=hops, cmap=plt.cm.Blues)
    plt.axis('equal')
    plt.show()
    

    你可以使用

    positions = nx.spring_layout(G)
    

    相反。我使用了 graphviz circo 布局,因为它在绘制我使用的平衡树方面做得更好。

    【讨论】:

    • 非常感谢。它看起来像我需要的,但我很难链接 pygraphviz 和 graphviz。我安装了两个(windows)并且我不断收到` raise ValueError("Program %s not found in path."%prog) ValueError: Program twopi not found in path.` ...我不知道要修改哪个文件链接graphviz的安装文件夹的路径。有什么线索吗?谢谢
    • 我知道有些人使用 PyGraphviz 或 Pydot 在 Windows 上工作。所以这是可能的 - 但我不熟悉如何正确安装它们。
    • stackoverflow.com/review/suggested-edits/21947989: 代码不适用于 networkx 2.2; # for networkx > 2 : positions = nx.nx_agraph.graphviz_layout(G, prog = 'twopi', root = 0)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多