Siddharth Satpathy
Siddharth Satpathy

Reputation: 3043

Visualization of social network from nodes and edges arrays

I want to visualize social networks using some tool (preferably some tool in python). Presently, I have my data in the form of arrays: an array with information about nodes (let’s give it the name Nodes). Each row of this array points to a node, while each column of this array points to a specific attribute. Values in each column of Nodes depict attribute values. Values of zero in this Nodes array are representative of missing attribute values.

Alongside the nodes array, I have an array for adjacency matrix (edges). Let’s call this array Edges. The Edges array is a square array of the same size as the number of rows (nodes) in Nodes array. This array (Edges) is filled with 0 and 1 as values. A value of 0 in (i,j) position of Edges would mean that the nodes i and j are not connected to each other. Whereas, a value of 1 in (m,n) position would imply that nodes m and n are connected to each other.

Here is a small illustrative example of arrays Nodes and Edges with 10 nodes:

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

In the data given above, we have 10 nodes and 3 attributes. How can I get a visualization of the network using these arrays (Nodes and Edges) ?

Upvotes: 0

Views: 1383

Answers (1)

busybear
busybear

Reputation: 10590

You should look into networkx. To create your graph directly from the adjacency matrix, you can use the function from_numpy_array.

import networkx as nx

adj = np.random.randint(2, size=(10,10))
G = nx.from_numpy_array(adj)

You can assign node attributes, but each attribute needs to have a name, which you haven't provided in your example. set_node_attributes makes it pretty to assign them though.

Visualizing it is also an option:

nx.draw(G, with_labels=True, font_weight='bold')

enter image description here

Upvotes: 2

Related Questions