Drawing lines between pairs in Python

I have a set of pairs:

pairs=[(3,6),(7,2),(8,5),(9,5),(5,13),(10,6),(6,1),(1,13),(11,2),(2,13),(12,4),(4,13)] 

Each pair describes a relationship between two points, i.e. there is a line between point 3 and point 6.

This is currently being done:

 i=0 for point in pairs: i+=1 plt.plot(point,(i,i)) plt.show() 

gives me straight lines between each point and its corresponding destination:

However, I am looking to connect these lines together to create a graph of "bridges", something like lines:

Thanks!

+7
source share
1 answer

Using networkx ,

 import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() edges = [ (3,6),(7,2),(8,5),(9,5),(5,13),(10,6),(6,1),(1,13),(11,2),(2,13),(12,4),(4,13)] G.add_edges_from(edges) nx.draw(G) plt.show() 

gives enter image description here

+5
source

All Articles