Как вы создаете сегменты линий между двумя точками?
У меня есть этот бит кода, который отображает точки:
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]()
но я хочу, чтобы все возможные строки были сделаны, что выглядит примерно так:
![enter image description here]()
Я пробовал путь matplotlib, но для меня это не сработало.
Ответы
Ответ 1
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]()
Ответ 2
Это может быть оптимизировано, но оно работает:
for point in all_data:
for point2 in all_data:
pyplot.plot([point[0], point2[0]], [point[1], point2[1]])
![enter image description here]()
Ответ 3
используя все комбинации?
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 combinations(all_data,2):
x.extend(i[0])
y.extend(i[1])
plot.plot(x,y)
pyplot.show()
Ответ 4
Другим способом может быть использование патчей matplotlib.
import matplotlib
import pylab as pl
fig, ax = pl.subplots()
import matplotlib.patches as patches
from matplotlib.path import Path
verts = [(x1,y1), (x2,y2)]
codes = [Path.MOVETO,Path.LINETO]
path = Path(verts, codes)
ax.add_patch(patches.PathPatch(path, color='green', lw=0.5))