【问题标题】:Visualization of social network with assignment of colors to nodes将颜色分配给节点的社交网络可视化
【发布时间】:2019-06-04 18:26:22
【问题描述】:

我想用节点和边可视化社交网络节点的颜色代表属性值。如果我可以使用 python 中的工具(如networkx)来实现,那就太好了,但我也对其他工具持开放态度(如Gephigraph-tools)。我在社交网络中拥有的节点和边采用 numpy 数组的形式。我希望这个可视化的节点根据属性值着色。

节点数组中的每一行都指向一个用户。节点数组中的每一列都指向一个属性。节点数组的每一列中的值都指向属性值。下面是一个包含 10 个用户和 3 个属性的节点数组示例(名称为 [Att1Att2Att3]。

Nodes = np.array([[1,2,4],[1,3,1],[2,2,1],[1,1,2],
              [1,2,2],[2,1,4],[1,2,1],[2,0,1],
              [2,2,4],[1,0,4]])

同样,边数组(邻接矩阵)是一个大小为节点数* 节点数的方阵。邻接矩阵中的值为 1 表示两个节点之间存在边,值为 0 表示不存在边。这是边数组的示例。

Edges = np.random.randint(2, size=(10,10))

假设我希望节点根据Nodes 中间列中给出的属性值着色(即Attribute_Value = Nodes[:,1] = [2, 3, 2, 1, 2, 1, 2, 0, 2, 0]) 有四个唯一的属性值[0,1,2,3] 所以,我希望有四种不同的颜色节点。在我的实际图表中,我有更多唯一的属性值。此外,我有数以万计的节点,所以我希望能够在我的图中调整节点的大小(半径)。

在我之前的帖子之后,我尝试了这个:

import networkx as nx
G = nx.from_numpy_array(Edges)
nx.draw(G, with_labels=True)

但是,上述代码 sn-p 的结果不允许我根据属性值选择颜色。另外,我需要调整节点的大小。如何以所述方式可视化社交图?

【问题讨论】:

    标签: python graph networkx gephi social-graph


    【解决方案1】:

    Networkx 允许可视化图形并指定节点的大小和颜色。 例如:

    import networkx as nx
    import matplotlib.pyplot as plt
    
    G = nx.barabasi_albert_graph(20, 2)
    node_colors = [x for x in range(20)]
    node_sizes = [50 * x for x in range(20)]
    cmap = plt.cm.jet
    nx.draw_networkx(G,
                     with_labels=True,
                     labels={node : 'some text {}'.format(node) for node in G.nodes()},
                     node_color=node_colors,
                     node_size=node_sizes,
                     cmap=cmap)
    plt.show()
    
    • labels - 是一个标签数组(按照G.nodes()的顺序)
    • node_sizes - 是一个整数数组,指定每个节点的大小
    • node_colors - 是指定每个节点颜色的数字数组
    • cmap - 将每个数字映射到特定颜色

    这会导致:

    要全面了解 Networkx 绘图的工作原理,我建议阅读 documentation

    就个人而言,为了探索和可视化特定图实例,我更喜欢将 networkx 图保存到文件中,并使用 gephi 加载它。如果您想为许多图形实例实现自动化流程,networkxs 可能会更好。

    如果您选择 gephi,只需加载一个图形并使用 GUI,这非常不言自明。

    【讨论】:

      【解决方案2】:

      networkx.draw 接受 node_colornode_size 的列表,这些列表需要与节点数一样长。因此,您只需将您的独特属性映射到某些颜色,然后创建这些列表。如果您有许多不同的属性,您将希望自动执行该映射。下面,我概述了 2 个选项,一个使用 matplotlib 颜色循环,另一个简单地将随机颜色分配给独特的属性。

      import numpy as np
      import matplotlib.pyplot as plt
      import networkx as nx
      
      Nodes = np.array([[1,2,4],
                        [1,3,1],
                        [2,2,1],
                        [1,1,2],
                        [1,2,2],
                        [2,1,4],
                        [1,2,1],
                        [2,0,1],
                        [2,2,4],
                        [1,0,4]])
      Edges = np.random.randint(2, size=(10,10))
      attribute_values = Nodes[:,1]
      
      # make a color mapping from node attribute to color
      
      # option 1: using the matplotlib color cycle;
      # however, you may run out of colors if there are many different unique attributes
      color_cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
      attribute_to_color = dict()
      for ii, attribute in enumerate(np.unique(attribute_values)):
          attribute_to_color[attribute] = color_cycle[ii]
      
      # option 2: assign random colors
      attribute_to_color = dict()
      for ii, attribute in enumerate(np.unique(attribute_values)):
          attribute_to_color[attribute] = np.random.rand(3)
      
      node_color = [attribute_to_color[attribute] for attribute in attribute_values]
      
      # adjust node sizes according to some other attribute
      node_size = Nodes[:, 2] * 100
      
      G = nx.from_numpy_matrix(Edges)
      nx.draw(G, node_color=node_color, node_size=node_size, with_labels=True)
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多