Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create line segments between two points?

I have this bit of code that plots out the points:

import matplotlib.pyplot as plot
from matplotlib import pyplot

all_data = [[1,10],[2,10],[3,10],[4,10],[5,10],[3,1],[3,2],[3,3],[3,4],[3,5]]
x = []
y = []
for i in xrange(len(all_data)):
    x.append(all_data[i][0])
    y.append(all_data[i][1])
plot.scatter(x,y)

pyplot.show()

what it shows

but I want all the possible lines that could be made that looks something like this:

enter image description here

I've tried matplotlib path, but it doesn't work well for me.

like image 265
George Avatar asked Jul 26 '11 18:07

George


People also ask

How do I construct a line segment?

Step 1: Draw a line of any length. Step 2: Mark the starting point of the line segment. Step 3: Take a ruler and place the pointer of the compass, apart from the pencil's lead. Step 4: Place the pointer of the compass at the starting point and mark an arc on the line with the pencil point.


2 Answers

This can be optimized but it works:

for point in all_data:
    for point2 in all_data:
        pyplot.plot([point[0], point2[0]], [point[1], point2[1]])

enter image description here

like image 101
joaquin Avatar answered Oct 23 '22 13:10

joaquin


import matplotlib.pyplot as plt
import itertools 

fig=plt.figure()
ax=fig.add_subplot(111)
all_data = [[1,10],[2,10],[3,10],[4,10],[5,10],[3,1],[3,2],[3,3],[3,4],[3,5]]
plt.plot(
    *zip(*itertools.chain.from_iterable(itertools.combinations(all_data, 2))),
    color = 'brown', marker = 'o')

plt.show()

enter image description here

like image 20
unutbu Avatar answered Oct 23 '22 12:10

unutbu