Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing lines between pairs in Python

I have a tuple 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 connection between two points, i.e there's a line between point 3 and point 6.

Currently, doing this:

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

is giving me straight lines between each point and its respective destination:

However, I'm looking for connecting these lines together to create a graph of "bridges", something along the lines of:

Thanks!

like image 258
IdoKendo Avatar asked Nov 07 '12 18:11

IdoKendo


People also ask

How do you draw lines in Python?

line() Draws a line between the coordinates in the xy list. Parameters: xy – Sequence of either 2-tuples like [(x, y), (x, y), …] or numeric values like [x, y, x, y, …].

How do you draw a straight line in Python?

Matplotlib: Graph/Plot a Straight Line The equation y=mx+c y = m x + c represents a straight line graphically, where m is its slope/gradient and c its intercept. In this tutorial, you will learn how to plot y=mx+b y = m x + b in Python with Matplotlib.


1 Answers

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

yields enter image description here

like image 150
unutbu Avatar answered Sep 20 '22 13:09

unutbu