1

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

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

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)

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

4

2 回答 2

2

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 绘图的工作原理,我建议阅读文档

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

如果您选择 gephi,只需加载图形并使用 GUI,这很容易解释。

于 2019-01-10T10:43:52.397 回答
1

networkx.draw接受列表,node_color并且node_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()
于 2019-01-10T10:42:22.027 回答