sridhar February 2016

Plot a connectivity graph with adjacency matrix and coordinates list in python

I have a set of points (x,y) coordinates and also adjacency matrix which gives details of connectivity. I want to plot the physical coordinates with the given connectivity. I know networkx is useful to create connectivity graph and scatter plot plots the physical coordinates. But what I want is both.

Answers


gcalmettes February 2016

You can plot both coordinates and connectivity with Networkx.

import networkx as nx
import numpy as np

pos = np.random.rand(10, 2) #coordinates, (x, y) for 10 nodes
connect = [tuple(np.random.random_integers(0, 9, size=(2))) for x in range(8)] #random connections

#creation of the graph
graph = nx.Graph()
#adding nodes/connections in the graph
for node in range(len(pos)):
    graph.add_node(node)
graph.add_edges_from(connect)

#plot of the nodes using the (x,y) pairs as coordinates
nx.draw(graph, [(x,y) for x,y in pos], node_size=50)

graph

Post Status

Asked in February 2016
Viewed 2,755 times
Voted 5
Answered 1 times

Search




Leave an answer