Visualization of social network with assignment of colors to nodes

时间:2019-01-09 22:24:46

标签: python graph networkx gephi social-graph

I want to visualize a social network with nodes and edges, with colors of nodes representing values of attributes. It will be nice if I can do it with tools in python (like networkx), but I am also open to other tools (like Gephi , graph-tools) . The nodes and edges that I have in my social network are in the form of numpy arrays. I want nodes of this visualization to be colored according to values of attributes.

Each row in the nodes array points to an user. Each column in the nodes array points to an attribute. The values in each column of the nodes array point to attribute values. Here is an example of a nodes array with 10 users and 3 attributes (with names [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]])

Similarly, the edges array (adjacency matrix) is a square array of size number of nodes * number of nodes. A value of 1 in the adjacency matrix points to a presence of an edge between two nodes, and a value of 0 points to absence of an edge. Here is an example of an edges array.

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

Let's say I want the nodes to be colored according to attribute values given in the middle column of Nodes (i.e. Attribute_Value = Nodes[:,1] = [2, 3, 2, 1, 2, 1, 2, 0, 2, 0]) There are four unique attribute values [0,1,2,3] so, I will like to have four different colors for the nodes. In my actual graph, I have many more unique values for attributes. Also, I have tens of thousands of nodes, so I will like to be able to adjust the sizes (radii) of nodes in my plot.

Following a previous post of mine, I have tried this:

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

But, results from the above code snippet do not let me choose colors as per attribute values. Also, I will need to adjust sizes of nodes. How can I visualize social graphs in the described way?

2 个答案:

答案 0 :(得分:1)

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()

答案 1 :(得分: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()
  • 标签-是一个标签数组(根据G.nodes()的顺序)
  • node_sizes -是一个整数数组,用于指定每个节点的大小
  • node_colors -是一个数字数组,用于指定每个节点的颜色
  • cmap -正在将每个数字映射到特定颜色

结果是: enter image description here

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

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

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